2 回答
TA贡献1998条经验 获得超6个赞
你的 json 是一个数组所以你需要使用 JArroy.Parse 而不是 JObject.Parse
代替
JObject googleSearch = JObject.Parse(googleSearchText);
为了
JArray googleSearch = JArray.Parse(googleSearchText);
然后您可以导航 de JArray 以查找您的值。
// get JSON result objects into a list IList<JToken> results =
var results = googleSearch.Last.ToList();
// serialize JSON results into .NET objects IList<SearchResult>
List<Country> countyList = new List<Country>();
foreach (JToken result in results)
{
// JToken.ToObject is a helper method that uses JsonSerializer internally
var country = new Country { Id = result["id"].ToString(), Value = result["name"].ToString() };
countyList.Add(country);
}
TA贡献1853条经验 获得超18个赞
虽然它是有效的 json,但序列化器需要一个根对象,因此如果您可以将 json 输出修改为根对象,您就可以正确地序列化它:
[
{
"page": {
"page":1,
"pages":31,
"per_page":"10",
"total":304
},
"data": [
{
"id":"ABW",
"iso2Code":"AW",
"name":"Aruba",
"region":{
"id":"LCN",
"iso2code":"ZJ",
"value":"Latin America & Caribbean "
},
"adminregion":{
"id":"",
"iso2code":"",
"value":""
},
"incomeLevel":{
"id":"HIC",
"iso2code":"XD",
"value":"High income"
},
"lendingType":{
"id":"LNX",
"iso2code":"XX",
"value":"Not classified"
},
"capitalCity":"Oranjestad",
"longitude":"-70.0167",
"latitude":"12.5167"
},
{
"id":"AFG",
"iso2Code":"AF",
"name":"Afghanistan",
"region":{
"id":"SAS",
"iso2code":"8S",
"value":"South Asia"
},
"adminregion":{
"id":"SAS",
"iso2code":"8S",
"value":"South Asia"
},
"incomeLevel":{
"id":"LIC",
"iso2code":"XM",
"value":"Low income"
},
"lendingType":{
"id":"IDX",
"iso2code":"XI",
"value":"IDA"
},
"capitalCity":"Kabul",
"longitude":"69.1761",
"latitude":"34.5228"
},
{
"id":"AFR",
"iso2Code":"A9",
"name":"Africa",
"region":{
"id":"NA",
"iso2code":"NA",
"value":"Aggregates"
},
"adminregion":{
"id":"",
"iso2code":"",
"value":""
},
"incomeLevel":{
"id":"NA",
"iso2code":"NA",
"value":"Aggregates"
},
"lendingType":{
"id":"",
"iso2code":"",
"value":"Aggregates"
},
"capitalCity":"",
"longitude":"",
"latitude":""
},
{
"id":"AGO",
"iso2Code":"AO",
"name":"Angola",
"region":{
"id":"SSF",
"iso2code":"ZG",
"value":"Sub-Saharan Africa "
},
"adminregion":{
"id":"SSA",
"iso2code":"ZF",
"value":"Sub-Saharan Africa (excluding high income)"
},
"incomeLevel":{
"id":"LMC",
"iso2code":"XN",
"value":"Lower middle income"
},
"lendingType":{
"id":"IBD",
"iso2code":"XF",
"value":"IBRD"
},
"capitalCity":"Luanda",
"longitude":"13.242",
"latitude":"-8.81155"
},
{
"id":"ALB",
"iso2Code":"AL",
"name":"Albania",
"region":{
"id":"ECS",
"iso2code":"Z7",
"value":"Europe & Central Asia"
},
"adminregion":{
"id":"ECA",
"iso2code":"7E",
"value":"Europe & Central Asia (excluding high income)"
},
"incomeLevel":{
"id":"UMC",
"iso2code":"XT",
"value":"Upper middle income"
},
"lendingType":{
"id":"IBD",
"iso2code":"XF",
"value":"IBRD"
},
"capitalCity":"Tirane",
"longitude":"19.8172",
"latitude":"41.3317"
},
{
"id":"AND",
"iso2Code":"AD",
"name":"Andorra",
"region":{
"id":"ECS",
"iso2code":"Z7",
"value":"Europe & Central Asia"
},
"adminregion":{
"id":"",
"iso2code":"",
"value":""
},
"incomeLevel":{
"id":"HIC",
"iso2code":"XD",
"value":"High income"
},
"lendingType":{
"id":"LNX",
"iso2code":"XX",
"value":"Not classified"
},
"capitalCity":"Andorra la Vella",
"longitude":"1.5218",
"latitude":"42.5075"
},
{
"id":"ANR",
"iso2Code":"L5",
"name":"Andean Region",
"region":{
"id":"NA",
"iso2code":"NA",
"value":"Aggregates"
},
"adminregion":{
"id":"",
"iso2code":"",
"value":""
},
"incomeLevel":{
"id":"NA",
"iso2code":"NA",
"value":"Aggregates"
},
"lendingType":{
"id":"",
"iso2code":"",
"value":"Aggregates"
},
"capitalCity":"",
"longitude":"",
"latitude":""
},
{
"id":"ARB",
"iso2Code":"1A",
"name":"Arab World",
"region":{
"id":"NA",
"iso2code":"NA",
"value":"Aggregates"
},
"adminregion":{
"id":"",
"iso2code":"",
"value":""
},
"incomeLevel":{
"id":"NA",
"iso2code":"NA",
"value":"Aggregates"
},
"lendingType":{
"id":"",
"iso2code":"",
"value":"Aggregates"
},
"capitalCity":"",
"longitude":"",
"latitude":""
},
{
"id":"ARE",
"iso2Code":"AE",
"name":"United Arab Emirates",
"region":{
"id":"MEA",
"iso2code":"ZQ",
"value":"Middle East & North Africa"
},
"adminregion":{
"id":"",
"iso2code":"",
"value":""
},
"incomeLevel":{
"id":"HIC",
"iso2code":"XD",
"value":"High income"
},
"lendingType":{
"id":"LNX",
"iso2code":"XX",
"value":"Not classified"
},
"capitalCity":"Abu Dhabi",
"longitude":"54.3705",
"latitude":"24.4764"
},
{
"id":"ARG",
"iso2Code":"AR",
"name":"Argentina",
"region":{
"id":"LCN",
"iso2code":"ZJ",
"value":"Latin America & Caribbean "
},
"adminregion":{
"id":"LAC",
"iso2code":"XJ",
"value":"Latin America & Caribbean (excluding high income)"
},
"incomeLevel":{
"id":"UMC",
"iso2code":"XT",
"value":"Upper middle income"
},
"lendingType":{
"id":"IBD",
"iso2code":"XF",
"value":"IBRD"
},
"capitalCity":"Buenos Aires",
"longitude":"-58.4173",
"latitude":"-34.6118"
}
]
}
]
课程:
public class RootObject
{
public Page page { get; set; }
public List<Datum> data { get; set; }
}
public class Page
{
public int page { get; set; }
public int pages { get; set; }
public string per_page { get; set; }
public int total { get; set; }
}
- 2 回答
- 0 关注
- 199 浏览
添加回答
举报