简体   繁体   中英

How to parse a JSON array inside a JSON object

I am calling a web service which returns the following JSON when I use

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

I have create d class like the following:

 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. 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 . Any help would be appreciated.

You need a root class. Your JSON contains a member named items

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

And then :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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