You should create a structure that represents JSON keys (in case if you exactly know it) and then you can easily deserialize JSON string into your structure. In my examle I've deserialized a response from Google Cloud Message server:
class templateResponse
{
public String multicast_id;
public String success;
public String failure;
public String canonical_ids;
public Result[] results;
public class Result
{
public String message_id;
public String registration_id;
public String error;
};
}
incoming JSON was:
"\"multicast_id\":7400896764380883211,\"success\":1,\"failure\":0,\"canonical_ids\":0,\"results\":[{\"message_id\":\"0:1351777805148960%39895cf0f9fd7ecd\"}]}"
So, use
templateResponse result = new JavaScriptSerializer().Deserialize<templateResponse>(json);
and you will get deserialized result
object
I would echo the Json.NET library, which can transform the JSON response into a XML document. With the XML document, you can easily query with XPath and extract the data you need. I find this pretty useful.
Just use the Json.NET library. It lets you parse Json format strings very easily:
JObject o = JObject.Parse(@"
{
""something"":""value"",
""jagged"":
{
""someother"":""value2""
}
}");
string something = (string)o["something"];
Documentation: Parsing JSON Object using JObject.Parse
I think this is what you want:
JavaScriptSerializer JSS = new JavaScriptSerializer();
T obj = JSS.Deserialize<T>(String);
Source: Stackoverflow.com