user13578
user13578

Reputation: 117

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.

Upvotes: 0

Views: 355

Answers (1)

Cid
Cid

Reputation: 15257

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());

Upvotes: 4

Related Questions