简体   繁体   English

如何解析 JSON 对象中的 JSON 数组

[英]How to parse a JSON array inside a JSON object

I am calling a web service which returns the following JSON when I use我正在调用一个 Web 服务,它在我使用时返回以下 JSON

dynamic jsonResult=    JsonConvert.DeserializeObject(response.Content.ToString());
{
    {  "items": 
      [ 
       {     
     "category": "Category1", 
         "word": "stars",  
        "lemma": "star",    
     "url": "XY"   
       }  
      ]
    }
  }

I have create d class like the following:我创建了如下所示的 d 类:

 public class MyClass
    {
        [JsonProperty("category")]
        public string category { get; set; }
  
        [JsonProperty("word")]
        public string word{ get; set; }

        [JsonProperty("lemma")]
        public string lemma{ get; set; }
        
        [JsonProperty("url")]
        public string url { get; set; }

But I don't know how I can extract the list of json objects inside the json array.但我不知道如何提取 json 数组中的 json 对象列表。 I have tried the following but it doesn't work:我已经尝试了以下但它不起作用:

List<MyClass> list = 
    JsonConvert.DeserializeObject<List<MyClass>>(response.Content.ToString());

I need to extract the result as a list of MyClass .我需要将结果提取为MyClass列表。 Any help would be appreciated.任何帮助,将不胜感激。

You need a root class.你需要一个根类。 Your JSON contains a member named items您的 JSON 包含一个名为items的成员

public class Root
{
    [JsonProperty("items")]
    public List<MyClass> items{ get; set; }
}

And then :进而 :

Root root = JsonConvert.DeserializeObject<Root>(response.Content.ToString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM