我正在使用以下代码来解析JSON。我有例外。
using System.Collections.Generic;
using Newtonsoft.Json;
using RestSharp;
using WebAutomation.BDD.Data;
namespace WebAutomation.Utils
{
public class RestApi
{
private static IRestResponse HttpGetResponse(string endPoint)
{
var client = new RestClient(endPoint);
var request = new RestRequest(Method.GET);
return client.Execute(request);
}
public static FeaturedMenuItems GetFeaturedMenuItems()
{
var a = HttpGetResponse("http://<DOMAIN>/api/web/menu/quick");
return JsonConvert.DeserializeObject<FeaturedMenuItems>(a.Content);
}
}
}
我正在使用以下课程来解析:
using System.Collections.Generic;
namespace WebAutomation.BDD.Data
{
public class FeaturedMenu
{
public string MenuItemName { get; set; }
public string MenuItemName_en { get; set; }
public string MenuItemName_zh { get; set; }
public string url { get; set; }
public string LastModifiedDate { get; set; }
}
public class FeaturedMenuItems
{
public List<FeaturedMenu> Featured_Menu { get; set; }
}
}
这是我得到的JSON:
[
{
"Featured_Menu": [
{
"MenuItemName": "Today's Racing",
"MenuItemName-en": "Today's Racing",
"MenuItemName-zh": "今日赛事",
"url": "/racing-betting/today",
"LastModifiedDate": "20170914111417"
},
{
"MenuItemName": "Tomorrow's Racing",
"MenuItemName-en": "Tomorrow's Racing",
"MenuItemName-zh": "明日赛事",
"url": "/racing-betting/tomorrow",
"LastModifiedDate": "20170914111417"
},
{
"MenuItemName": "Upcoming Sports",
"MenuItemName-en": "Upcoming Sports",
"MenuItemName-zh": "近期赛事",
"url": "/sports-betting/next-up",
"LastModifiedDate": "20170914111417"
},
{
"MenuItemName": "Live Centre",
"MenuItemName-en": "Live Centre",
"MenuItemName-zh": "即场投注",
"url": "/live-in-play-betting",
"LastModifiedDate": "20170914111417"
},
{
"MenuItemName": "Multi Express",
"MenuItemName-en": "Multi Express",
"MenuItemName-zh": "Multi Express",
"url": "/multi-express",
"LastModifiedDate": "20170914111417"
},
{
"MenuItemName": "AFL Finals",
"MenuItemName-en": "AFL Finals",
"MenuItemName-zh": "AFL Finals",
"url": "/sports-betting/australian-rules/afl",
"LastModifiedDate": "20170914111417"
},
{
"MenuItemName": "NRL Finals",
"MenuItemName-en": "NRL Finals",
"MenuItemName-zh": "NRL Finals",
"url": "/sports-betting/rugby-league/nrl/nrl-matches",
"LastModifiedDate": "20170914111417"
},
{
"MenuItemName": "MLB Matches",
"MenuItemName-en": "MLB Matches",
"MenuItemName-zh": "MLB Matches",
"url": "/sports-betting/baseball/major-league-baseball/mlb-matches",
"LastModifiedDate": "20170914111417"
},
{
"MenuItemName": "NFL Matches",
"MenuItemName-en": "NFL Matches",
"MenuItemName-zh": "NFL Matches",
"url": "/sports-betting/american-football/nfl/nfl-matches/",
"LastModifiedDate": "20170914111417"
},
{
"MenuItemName": "EPL Matches",
"MenuItemName-en": "EPL Matches",
"MenuItemName-zh": "EPL Matches",
"url": "/sports-betting/soccer/united-kingdom/english-premier-league-matches",
"LastModifiedDate": "20170914111417"
},
{
"MenuItemName": "UEFA Europa League",
"MenuItemName-en": "UEFA Europa League",
"MenuItemName-zh": "UEFA Europa League",
"url": "/sports-betting/soccer/uefa-competitions/europa-league-matches/",
"LastModifiedDate": "20170914111417"
},
{
"MenuItemName": "Golovkin v Alvarez",
"MenuItemName-en": "Golovkin v Alvarez",
"MenuItemName-zh": "Golovkin v Alvarez",
"url": "/sports-betting/boxing/bouts/fights/gennady-golovkin-v-saul-alvarez-20170917-600896-21745986",
"LastModifiedDate": "20170914111417"
},
{
"MenuItemName": "Makybe Diva Stakes",
"MenuItemName-en": "Makybe Diva Stakes",
"MenuItemName-zh": "Makybe Diva Stakes",
"url": "/racing-betting/horse-racing/flemington/20170916/race-7-704632-23756853",
"LastModifiedDate": "20170914111417"
},
{
"MenuItemName": "The Shorts",
"MenuItemName-en": "The Shorts",
"MenuItemName-zh": "The Shorts",
"url": "/racing-betting/horse-racing/randwick/20170916/race-7-704601-23756572",
"LastModifiedDate": "20170914111417"
},
{
"MenuItemName": "Lotto",
"MenuItemName-en": "Lotto",
"MenuItemName-zh": "Lotto",
"url": "/http://crownlotto.com.au",
"LastModifiedDate": "20170914111417"
}
]
}
]
我得到以下例外:
newtonsoft.json.jsonserializationException:' 当前的JSON数组(例如[1,2,3]) 'webautomation.bdd.data.featuredmenuitems',因为该类型需要一个 json对象(例如{" name":" value"})正确序列化。修理 此错误要么将JSON更改为JSON对象(例如 {" name":" value"})或将避难类型更改为数组或一个 键入实现集合接口(例如Icollection,iList)的类型 喜欢从JSON阵列中进行的列表。 JSONARRAYATTRIBUTE也可以添加到该类型中,以将其强加于 从JSON阵列中进行挑选。路径'',第1行,位置1。'
我无法弄清楚这个问题。任何帮助都将不胜感激。
您正在尝试对FeaturedMenuItems
的数组进行挑选,而不是单个FeaturedMenuItems
。尝试使用:
JsonConvert.DeserializeObject<FeaturedMenuItems[]>(a.Content)