JObject中的嵌套数组

时间:2017-02-07 12:31:08

标签: c# json asp.net-mvc api json.net

我正在使用OutBrain Api。

在我的代码中,函数response.Content.ReadAsStringAsync();返回一个json对象,如下所示:

{
  "results": [
    {
      "metadata": {
        "id": "2016-10",
        "fromDate": "2016-10-01",
        "toDate": "2016-10-30"
      },
      "metrics": {
        "impressions": 1164823829,
        "clicks": 2660235,
        "conversions": 2228,
        "spend": 463546.37,
        "ecpc": 0.17,
        "ctr": 0.23,
        "conversionRate": 0.08,
        "cpa": 208.05
      }
    },
    {
      "metadata": {
        "id": "2016-09",
        "fromDate": "2016-09-01",
        "toDate": "2016-09-30"
      },
      "metrics": {
        "impressions": 959483548,
        "clicks": 2245759,
        "conversions": 1777,
        "spend": 385899.67,
        "ecpc": 0.17,
        "ctr": 0.23,
        "conversionRate": 0.08,
        "cpa": 217.16
      }
    },
    {
      "metadata": {
        "id": "2016-08",
        "fromDate": "2016-08-01",
        "toDate": "2016-08-31"
      },
      "metrics": {
        "impressions": 980319229,
        "clicks": 2621017,
        "conversions": 1818,
        "spend": 358970.61,
        "ecpc": 0.14,
        "ctr": 0.27,
        "conversionRate": 0.07,
        "cpa": 197.45
      }
    }
  ],
  "totalResults": 3,
  "summary": {
    "impressions": 3104626606,
    "clicks": 7527011,
    "conversions": 5823,
    "spend": 1208416.65,
    "ecpc": 0.16,
    "ctr": 0.24,
    "conversionRate": 0.08,
    "cpa": 207.52
  }
}

我正在尝试访问每个"元数据中的数据"和"指标"这些都在"结果"但似乎我的代码不起作用。

我的代码:

string responseData = await response.Content.ReadAsStringAsync();

                    JObject campaignData = JsonConvert.DeserializeObject<dynamic>(responseData);

                    Dictionary<string, dynamic> mediaCampaigns = new Dictionary<string, dynamic>();
                    if (campaignData != null)
                    {
                        if (campaignData["totalResults"].ToString() != "1" & campaignData["totalResults"].ToString() != "0")
                        {
                            foreach (var campItem in campaignData)
                            {
                                mediaCampaigns.Add(campItem["@results"]["@metadata"]["@fromDate"]ToString(), new
                                {
                                    cost = campaignData["@results"]["@metrics"]["@spend"] != null ? campaignData["@results"]["@metrics"]["@spend"] : 0,
                                    clicks = campaignData["@results"]["@metrics"]["@clicks"] != null ? campaignData["@results"]["@metrics"]["@clicks"] : 0,
                                    impressions = campaignData["@results"]["@metrics"]["@impressions"] != null ? campaignData["@results"]["@impressions"]["@impressions"] : 0,
                                    conversions = campaignData["@results"]["@metrics"]["@conversions"] != null ? campaignData["@results"]["@metrics"]["@conversions"] : 0,
                                });
                            }
                        }
                        else
                        {
                            mediaCampaigns.Add(campaignData["@results"]["@metadata"]["@fromDate"].ToString(), new
                            {
                                cost = campaignData["@results"]["@metrics"]["@spend"] != null ? campaignData["@results"]["@metrics"]["@spend"] : 0,
                                clicks = campaignData["@results"]["@metrics"]["@clicks"] != null ? campaignData["@results"]["@metrics"]["@clicks"] : 0,
                                impressions = campaignData["@results"]["@metrics"]["@impressions"] != null ? campaignData["@results"]["@impressions"]["@impressions"] : 0,
                                conversions = campaignData["@results"]["@metrics"]["@conversions"] != null ? campaignData["@results"]["@metrics"]["@conversions"] : 0,
                            });
                        }
                    }

我想得到一些帮助,谢谢!

1 个答案:

答案 0 :(得分:1)

在以下代码中,RootObject是由this工具生成的类:

var test = JsonConvert.DeserializeObject<RootObject>(json);
Dictionary<string, dynamic> mediaCampaigns = new Dictionary<string, dynamic>();
foreach (var item in test.results)
{
    mediaCampaigns.Add(item.metadata.fromDate, new 
    {
        cost = item.metrics.spend,
        clicks = item.metrics.clicks,
        impressions = item.metrics.impressions,
        conversions = item.metrics.conversions
    });
}

这对我有用。也改变

ReadAsStringAsync()

ReadAsStringAsync().Result;