带索引键的Newtonsoft数组反序列化

时间:2014-03-10 08:39:03

标签: c# arrays json serialization

我使用newtonsoft.Net库来反序列化/序列化对象。

我可以将以下json反序列化为“OfferPixel”数组吗?

所有数组都有服务上每个项目的索引号。所以“Offerpixel”对象似乎是索引号的子项。

{
"data": {
      "1": {
        "OfferPixel": {
          "id": "1",
          "affiliate_id": "1009",
          "offer_id": "7",
          "status": "deleted",
          "code": "",
          "type": "url",
          "modified": "2012-02-16 10:07:33",
          "goal_id": null
        }
      },
      "2": {
        "OfferPixel": {
          "id": "2",
          "affiliate_id": "1011",
          "offer_id": "7",
          "status": "deleted",
          "code": "",
          "type": "code",
          "modified": "2013-08-16 07:27:20",
          "goal_id": null
        }
      },
      "3": {
        "OfferPixel": {
          "id": "3",
          "affiliate_id": "1010",
          "offer_id": "7",
          "status": "deleted",
          "code": "",
          "type": "image",
          "modified": "2013-01-31 12:01:57",
          "goal_id": null
        }
       },
    "errors": [],
    "errorMessage": null
  }
}

1 个答案:

答案 0 :(得分:1)

使用Json.Net

var list = JObject.Parse(json)
                    .Descendants()
                    .OfType<JProperty>()
                    .Where(p => p.Name == "OfferPixel")
                    .Select(x => x.Value.ToObject<OfferPixel>())
                    .ToList();

public class OfferPixel
{
    public string id { get; set; }
    public string affiliate_id { get; set; }
    public string offer_id { get; set; }
    public string status { get; set; }
    public string code { get; set; }
    public string type { get; set; }
    public string modified { get; set; }
    public object goal_id { get; set; }
}