How can I deserialize array of json objects in c#? Here's my json:
[{"id":"255521115", "user":"username","userinfo":{"id":"158","online":"false"}}]
I have this code to get username:
[JsonProperty("user")]
public string Username { get; set; }
How can I get values of the userinfo? I want online value that is inside userinfo object.
There is one cool feature in VS2013. If you copy your JSON to clipboard and in Visual Studio click EDIT -> Paste Special -> Paste JSON as Classes it will generate class structure for you. All you need is to rename some properties if you want to. In your case modified version of classes will be:
public class Obj
{
public string Id { get; set; }
[JsonProperty("User")]
public string UserName { get; set; }
public Userinfo Userinfo { get; set; }
}
public class Userinfo
{
public string Id { get; set; }
public string Online { get; set; }
}
And then you can easily deserialize your JSON string:
var json = @"[{""id"":""255521115"", ""user"":""username"",""userinfo"":{""id"":""158"",""online"":""false""}}]";
var objs = JsonConvert.DeserializeObject<IList<Obj>>(json);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With