【问题标题】:Newtonsoft Array With Index Key Deserialize具有索引键反序列化的 Newtonsoft 数组
【发布时间】:2014-03-10 08:39:03
【问题描述】:

我使用 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
  }
}

【问题讨论】:

    标签: c# arrays json serialization


    【解决方案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; }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-07-07
      • 2021-10-13
      • 2017-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多