I'm using Json.NET (http://james.newtonking.com/projects/json/help/) as a way to serialize and deserialize JSON from a server. Let's say I have the following JSON object:
{
"user" : {
"name" : "Bob",
"age" : 35
},
"location" : "California"
}
The only way that I can find to deserialize this in to native types is to use a custom DTO as follows:
string jsonString = ""; // json string from above
Response result = JsonConvert.DeserializeObject<Response> (jsonString);
where the Response class looks something like:
public class Response
{
[JsonProperty("user")]
public UserResponse User { get; set; }
[JsonProperty("location")]
public string Location { get; set; }
}
public class UserResponse
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("age")]
public int Age { get; set; }
}
I want to deserialize this in to native types but I'm in an environment where I don't always know what the JSON string will look like... so it's hard to use custom DTO's when I don't know exactly what I'm getting down the pipe. If I don't pass any class in to JsonConvert.DeserializeObject(), then I end up with Json.NET types instead of native types like strings, ints, etc. Any suggestions on how to get around this? Should I be using a different JSON library instead?
Thanks!