0

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.

3
  • 2
    That ain't JSON. Notice how it's wrapped by double curlies? {{ ... }}. Not legal. Commented Aug 26, 2020 at 8:51
  • @spender IMO, this is just a typo when adding the example Commented Aug 26, 2020 at 8:54
  • @Cid Assumptions cause wasted time. We shouldn't need to form "opinions" about what OP intended to communicate. Commented Aug 26, 2020 at 9:01

1 Answer 1

4

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());
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.