Newtonsoft数组与索引键反序列化

本文关键字:反序列化 索引 数组 Newtonsoft | 更新日期: 2023-09-27 17:49:18

我用newtonsoft。用于反序列化/序列化对象的。Net库。

我可以反序列化以下json数组的"OfferPixel"?

所有数组都有服务上每个Item的索引号。因此"Offerpixel"对象似乎是Index Number的子条目。

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

Newtonsoft数组与索引键反序列化

使用Json。净

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; }
}