Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在mvc中使用newtonsoft反序列化嵌套json字符串#_C#_Arrays_Json_Asp.net Mvc 5_Json Deserialization - Fatal编程技术网

C# 如何在mvc中使用newtonsoft反序列化嵌套json字符串#

C# 如何在mvc中使用newtonsoft反序列化嵌套json字符串#,c#,arrays,json,asp.net-mvc-5,json-deserialization,C#,Arrays,Json,Asp.net Mvc 5,Json Deserialization,我一直在尝试反序列化嵌套的json。我正在从中获取JSON。我想从JSON中获取国家id,并将其值绑定到dropdownlist中 我不确定,怎么了 其他信息 要反序列化的代码 JObject googleSearch = JObject.Parse(googleSearchText); // get JSON result objects into a list IList<JToken> results = googleSearch["responseData"]["resul

我一直在尝试反序列化嵌套的json。我正在从中获取JSON。我想从JSON中获取
国家id
,并将其值绑定到dropdownlist中

我不确定,怎么了

其他信息 要反序列化的代码

JObject googleSearch = JObject.Parse(googleSearchText);

// get JSON result objects into a list IList<JToken> results =
googleSearch["responseData"]["results"].Children().ToList();

// serialize JSON results into .NET objects IList<SearchResult>
searchResults = new List<SearchResult>(); foreach (JToken result in results) {
// JToken.ToObject is a helper method that uses JsonSerializer internally
SearchResult searchResult = result.ToObject<SearchResult>();
searchResults.Add(searchResult); }
给出下面的代码。这是我的模型:

Using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

    namespace ejemplo.Models
   {
    public class Paginacion
    {
        public Paginacion()
        {
            this.List = new List<Data>();
        }

        [JsonProperty("page")]
        public int Page { get; set; }

        [JsonProperty("pages")]
        public int Pages { get; set; }

        [JsonProperty("per_page")]
        public string PerPage { get; set; }

        [JsonProperty("total")]
        public int Total { get; set; }

        public List<Data> List { get; set; }
    }

    public class Country
    {
        [JsonProperty("id")]
        public string Id { get; set; }

        [JsonProperty("value")]
        public string Value { get; set; }
    }

    public class Indicator
    {
        [JsonProperty("id")]
        public string Id { get; set; }

        [JsonProperty("value")]
        public string Value { get; set; }
    }

public class Data
{
    public Data()
    {
        this.Indicator = new Indicator();
        this.Country = new Country();
    }

    [JsonProperty("indicator")]
    public Indicator Indicator { get; set; }

    [JsonProperty("country")]
    public Country Country { get; set; }

    [JsonProperty("date")]
    public string Date { get; set; }

    [JsonProperty("value")]
    public string Value { get; set; }

    [JsonProperty("decimal")]
    public string Decimal { get; set; }

   }
}
使用Newtonsoft.Json;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
名称空间ejempo.Models
{
公共类分页
{
公共页码()
{
this.List=新列表();
}
[JsonProperty(“第页”)]
公共整型页{get;set;}
[JsonProperty(“页面”)]
公共整型页{get;set;}
[JsonProperty(“每页”)]
每个页面的公共字符串{get;set;}
[JsonProperty(“总计”)]
公共整数总计{get;set;}
公共列表{get;set;}
}
公营国家
{
[JsonProperty(“id”)]
公共字符串Id{get;set;}
[JsonProperty(“价值”)]
公共字符串值{get;set;}
}
公共类指标
{
[JsonProperty(“id”)]
公共字符串Id{get;set;}
[JsonProperty(“价值”)]
公共字符串值{get;set;}
}
公共类数据
{
公共数据()
{
this.Indicator=newindicator();
this.Country=新国家();
}
[JsonProperty(“指标”)]
公共指示符{get;set;}
[JsonProperty(“国家”)]
公共国家{get;set;}
[JsonProperty(“日期”)]
公共字符串日期{get;set;}
[JsonProperty(“价值”)]
公共字符串值{get;set;}
[JsonProperty(“十进制”)]
公共字符串十进制{get;set;}
}
}

虽然它是有效的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; }
}
公共类根对象
{
公共页页面{get;set;}
公共列表数据{get;set;}
}
公共类页面
{
公共整型页{get;set;}
公共整型页{get;set;}
每个页面的公共字符串{get;set;}
公共整数总计{get;set;}
}

您的json是一个数组,因此需要使用JArroy.Parse而不是JObject.Parse

替换

JObject googleSearch = JObject.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);
}
//将JSON结果对象放入IList结果列表中=
var results=googleSearch.Last.ToList();
//将JSON结果序列化为.NET对象IList
List countyList=新列表();
foreach(JToken result in results)
{
//ToObject是一个在内部使用JsonSerializer的助手方法
var country=new country{Id=result[“Id”].ToString(),Value=result[“name”].ToString()};
countyList.Add(国家);
}

是否出现错误?请尝试使用此@MarkC生成模型。我在第行得到了错误:Info=JsonConvert.DeserializeObject(json);无法将当前JSON数组(例如[1,2,3])反序列化为类型“eJompo.Models.PaginAction”,因为该类型需要一个JSON对象(例如{“name”:“value”})才能正确反序列化。
JArray googleSearch = JArray.Parse(googleSearchText);
// 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);
}