I'm trying to parse a json from [https://reddit.com/new/.json], using c# and json.net. The problem is the json is different for each post, and I need to know if is there a way do dinamically deserialize the json. Anyone?
Asked
Active
Viewed 187 times
1 Answers
3
Does the JSON being returned keep the same object names though? Just sometimes it may be blank or may have values?
If the same objects are always there you can just do something like this and it should work for you.
public class Account
{
public string Email { get; set; }
public bool Active { get; set; }
public DateTime CreatedDate { get; set; }
public IList<string> Roles { get; set; }
}
string json = @"{
'Email': 'james@example.com',
'Active': true,
'CreatedDate': '2013-01-20T00:00:00Z',
'Roles': [
'User',
'Admin'
]
}";
Account account = JsonConvert.DeserializeObject<Account>(json);
Console.WriteLine(account.Email);
// james@example.com
http://www.newtonsoft.com/json/help/html/DeserializeObject.htm
-
Rfjt Thanks for the answer, the dificult is that the json response changes. The pattern is not clear. That's my dificult. But I tried a new approach, instead parse the received json, I choose some basic properties and parse the response. Worked. Thanks for your return. – Gerson C Filho Apr 17 '17 at 05:13