Tal Tal - 1 year ago 54
JSON Question

Nested array in JObject

I am working with OutBrain Api .

In my code, the function

response.Content.ReadAsStringAsync();
returns a json object, which looks like this:

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


I am trying to reach the data inside every "metadata" and "metrics" which are all in the "results" but it seems that my code doesn't work.

My Code:

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


I would like to get some help, thanks!

Answer Source

In the following code the RootObject is a class generated by this tool:

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

This works for me. Also change

ReadAsStringAsync()

to

ReadAsStringAsync().Result;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download