0

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!

5
  • What is the error you get? Commented Feb 7, 2017 at 13:17
  • I just cant access the data inside the json object. the foreach loop put nothing in the Dictionary . Commented Feb 7, 2017 at 13:23
  • 1
    Why don't you deserialize it into an object? Is there any specific reason? Commented Feb 7, 2017 at 13:30
  • ok i could try , but how its will help me ? Commented Feb 7, 2017 at 13:33
  • you can access it from the list once you deserialized inside foreach, try the intellisense it helps Commented Feb 7, 2017 at 13:39

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks ! its helped me !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.