Parse Json Array in class in C #

I analyzed this single Json:

{ "text": "Sample Text", "id": 123456789, "user": { "name": "ExampleUser", "id": 123, "screen_name": "ExampleUser" }, "in_reply_to_screen_name": null, } 

for my C # RootObject class:

 public class User { public string name { get; set; } public int id { get; set; } public string screen_name { get; set; } } public class RootObject { public string text { get; set; } public long id { get; set; } public User user { get; set; } public object in_reply_to_screen_name { get; set; } } 

like this:

 RootObject h = JsonConvert.DeserializeObject<RootObject>(string); 

It all worked fine, but now I would like to parse an array of all the json predecessor objects.

For example, this Json array:

 [ { "text": "Sample Text", "id": 123456789, "user": { "name": "ExampleUser", "id": 123, "screen_name": "ExampleUser" }, "in_reply_to_screen_name": null, }, { "text": "Another Sample Text", "id": 101112131415, "user": { "name": "ExampleUser2", "id": 124, "screen_name": "ExampleUser2" }, "in_reply_to_screen_name": null, } ] 

I create another class:

  public class ListRoot { public List<RootObject> status { get; set; } } 

and then used the same method:

 ListRoot h = JsonConvert.DeserializeObject<ListRootObject>(string); 

But that will not work. Do you know how I can parse this Json array to a C # class?

+6
source share
1 answer

The JSON you have will work if you just deserialize it as a List<RootObject> :

 var h = JsonConvert.DeserializeObject<List<RootObject>>(string); 

Or an array:

 var h = JsonConvert.DeserializeObject<RootObject[]>(string); 

If you want to deserialize ListRoot , JSON should look like this:

 { "status": [ { "text": "Sample Text", ... }, { "text": "Another Sample Text", ... } ] } 
+10
source

All Articles