You can use the C# dynamic type to make things easier. This technique also makes re-factoring simpler as it does not rely on magic-strings.
JSON
The JSON string below is a simple response from an HTTP API call, and it defines two properties: Id and Name.
{"Id": 1, "Name": "biofractal"}
C#
Use JsonConvert.DeserializeObject<dynamic>() to deserialize this string into a dynamic type then simply access its properties in the usual way.
dynamic results = JsonConvert.DeserializeObject<dynamic>(json);
var id = results.Id;
var name= results.Name;
If you specify the type of the results variable as dynamic, instead of using the var keyword, then the property values will correctly deserialize, e.g. Id to an int and not a JValue (thanks to GFoley83 for the comment below).
Note: The NuGet link for the Newtonsoft assembly is http://nuget.org/packages/newtonsoft.json.
Package: You can also add the package with nuget live installer, with your project opened just do browse package and then just install it install, unistall, update, it will just be added to your project under Dependencies/NuGet
Answer from biofractal on Stack OverflowVideos
You can use the C# dynamic type to make things easier. This technique also makes re-factoring simpler as it does not rely on magic-strings.
JSON
The JSON string below is a simple response from an HTTP API call, and it defines two properties: Id and Name.
{"Id": 1, "Name": "biofractal"}
C#
Use JsonConvert.DeserializeObject<dynamic>() to deserialize this string into a dynamic type then simply access its properties in the usual way.
dynamic results = JsonConvert.DeserializeObject<dynamic>(json);
var id = results.Id;
var name= results.Name;
If you specify the type of the results variable as dynamic, instead of using the var keyword, then the property values will correctly deserialize, e.g. Id to an int and not a JValue (thanks to GFoley83 for the comment below).
Note: The NuGet link for the Newtonsoft assembly is http://nuget.org/packages/newtonsoft.json.
Package: You can also add the package with nuget live installer, with your project opened just do browse package and then just install it install, unistall, update, it will just be added to your project under Dependencies/NuGet
If you just need to get a few items from the JSON object, I would use Json.NET's LINQ to JSON JObject class. For example:
JToken token = JObject.Parse(stringFullOfJson);
int page = (int)token.SelectToken("page");
int totalPages = (int)token.SelectToken("total_pages");
I like this approach because you don't need to fully deserialize the JSON object. This comes in handy with APIs that can sometimes surprise you with missing object properties, like Twitter.
Documentation: Serializing and Deserializing JSON with Json.NET and LINQ to JSON with Json.NET
You can implement a class that holds the fields you have in your JSON
class MyData
{
public string t;
public bool a;
public object[] data;
public string[][] type;
}
and then use the generic version of DeserializeObject:
MyData tmp = JsonConvert.DeserializeObject<MyData>(json);
foreach (string typeStr in tmp.type[0])
{
// Do something with typeStr
}
Documentation: Serializing and Deserializing JSON
A much easier solution: Using a dynamic type
As of Json.NET 4.0 Release 1, there is native dynamic support.
You don't need to declare a class, just use dynamic :
dynamic jsonDe = JsonConvert.DeserializeObject(json);
All the fields will be available:
foreach (string typeStr in jsonDe.Type[0])
{
// Do something with typeStr
}
string t = jsonDe.t;
bool a = jsonDe.a;
object[] data = jsonDe.data;
string[][] type = jsonDe.Type;
With dynamic you don't need to create a specific class to hold your data.