当值从int更改为array + order时,如何序列化json可能会更改

时间:2010-09-20 05:23:22

标签: c# json serialization

我不确定如何将以下示例从json序列化为c#对象。通常,我会这样做:

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

但在这种情况下,我不确定如何创建类以使其基础,因为值和顺序可能会发生变化。

以下是示例:

    {
       "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)"
          },

问题是,在最后一个结果中,值会从

更改
 "value": 0,

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

此外,返回的JSON的顺序可能会更改...

因此,我不确定如何序列化这个...有没有人有任何建议?

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

您正在解析的JSON是对象

您的反序列化代码将更改为

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

关于,您可以共享 Library.Libraries.Model.Post 的类定义吗?