Home Ask Login Register

Developers Planet

Your answer is one click away!

Angshu Guha February 2016

Deserialize complex JSON data in C#

I'm just an amateur in C# programming. Now i have a JSON data that looks like following

  type: "xxx",
  width: "xxx",
  dataSource: {
      "chart": {
          "caption": "xxx"
      "data": [

I'm having the whole data as escaped string. now after Unescape when I'm using JavaScriptSerializer as follows

var data = ser.Deserialize<Dictionary<String, Object>>(chartData);

I'm able to get the "type", "width" as


Now I have to get the value of "caption". Any suggestion how to get that, I believe the dictionary structure need to be changed but I'm stacked for my lack of knowledge in C#


Amir Popovich February 2016

If you know the object's scheme you man want to create a class that represents in and then deserialize the json into it:

YourKnownClass obj = JsonConvert.DeserializeObject<YourKnownClass>(json);

Another option is by using a dynamic type (There is no good reason using a dynamic object if you know the schema and can create a matching C# class. This has a performance impact as well):

dynamic obj = JsonConvert.DeserializeObject<dynamic>(json);

BTW, In this example i'm using json.net which is a popular library.

Post Status

Asked in February 2016
Viewed 3,163 times
Voted 11
Answered 1 times


Leave an answer

Quote of the day: live life