views:

34

answers:

2

I'm not sure how to serialize the following example from json to a c# object. Normally, I do something like this:

 JavaScriptSerializer ser = new JavaScriptSerializer();
 result = ser.Deserialize<Library.Libraries.Model.Post>(responseFromServer);

But in this case, I'm not sure how to create the class to base it off since the values and order might change.

Here's the example:

    {
       "data": [
          {
             "id": "0000000/insights/page_fan_adds_unique/day",
             "name": "page_fan_adds_unique",
             "period": "day",
             "values": [
                {
                   "value": 2,
                   "end_time": "2010-09-15T07:00:00+0000"
                },
                {
                   "value": 0,
                   "end_time": "2010-09-16T07:00:00+0000"
                },
                {
                   "value": 1,
                   "end_time": "2010-09-17T07:00:00+0000"
                }
             ],
             "description": "Daily New Likes of your Page (Unique Users)"
          },
          {
             "id": "0000000/insights/page_fan_adds/day",
             "name": "page_fan_adds",
             "period": "day",
             "values": [
                {
                   "value": 2,
                   "end_time": "2010-09-15T07:00:00+0000"
                },
                {
                   "value": 0,
                   "end_time": "2010-09-16T07:00:00+0000"
                },
                {
                   "value": 1,
                   "end_time": "2010-09-17T07:00:00+0000"
                }
             ],
             "description": "Daily New Likes of your Page (Total Count)"
          },
          {
             "id": "0000000/insights/page_fan_removes_unique/day",
             "name": "page_fan_removes_unique",
             "period": "day",
             "values": [
                {
                   "value": 0,
                   "end_time": "2010-09-15T07:00:00+0000"
                },
                {
                   "value": 1,
                   "end_time": "2010-09-16T07:00:00+0000"
                },
                {
                   "value": 0,
                   "end_time": "2010-09-17T07:00:00+0000"
                }
             ],
             "description": "Daily Unlikes of your Page (Unique Users)"
          },
          {
             "id": "0000000/insights/page_fan_removes/day",
             "name": "page_fan_removes",
             "period": "day",
             "values": [
                {
                   "value": 0,
                   "end_time": "2010-09-15T07:00:00+0000"
                },
                {
                   "value": 1,
                   "end_time": "2010-09-16T07:00:00+0000"
                },
                {
                   "value": 0,
                   "end_time": "2010-09-17T07:00:00+0000"
                }
             ],
             "description": "Daily Unlikes of your Page (Total Count)"
          },
          {
             "id": "0000000/insights/page_fan_adds_source_unique/day",
             "name": "page_fan_adds_source_unique",
             "period": "day",
             "values": [
                {
                   "value": {
                      "fan_box": 1,
                      "fan_page": 1
                   },
                   "end_time": "2010-09-15T07:00:00+0000"
                },
                {
                   "value": [

                   ],
                   "end_time": "2010-09-16T07:00:00+0000"
                },
                {
                   "value": {
                      "fan_page": 1
                   },
                   "end_time": "2010-09-17T07:00:00+0000"
                }
             ],
             "description": "Daily Users can like your page in many different places, both within Facebook and on other websites.  These are the most common places where users like your Page. (Unique Users)"
          },

The problem is that on the last result the value changes from

 "value": 0,

to

 "value": {
            "fan_box": 1,
            "fan_page": 1
          },

Also, the order of the returned JSON might be changed...

As such, I'm not sure how to go about serializing this... Does anyone have any suggestions?

A: 

You can use JavaScriptSerializer.DeserializeObject.

VinayC
A: 

The JSON you are typring to parse is of objects

your deserialization code will change to

result = ser.Deserialize<List<Library.Libraries.Model.Post>>(responseFromServer);

regarding the value thing can you share class definition of Library.Libraries.Model.Post?

ajay_whiz