0

How to read a Json string with dynamic Node using c# ? I am not able to read the keys and sub nodes in the key.

      {
         "2030417": [{
          "country": "Malaysia",
          "push": 20543,
          "click": 752,
          "ctr": 3.66,
          "cpc": 0.03,
          "conversion": 0,
          "conversionrate": 0,
          "cpa": 0,
          "SOV%": "3.87"
         }],
         "2032769": [{
          "country": "India",
          "push": 10460,
          "click": 0,
          "ctr": 0,
          "cpc": 0.001,
          "conversion": 7,
          "conversionrate": 0.07,
          "cpa": 2.22,
          "SOV%": "0.28"
         }]
        }
pavan kumar
  • 322
  • 2
  • 7

1 Answers1

1

You can either:

Use the dynamic object & call properties on the fly:

dynamic d = JObject.Parse("{number:1000, str:'string', array: [1,2,3,4,5,6]}");

Console.WriteLine(d.number);
Console.WriteLine(d.str);
Console.WriteLine(d.array.Count);

You can also iterate through different properties if needed:

JObject d = JObject.Parse("{\"2030417\":[{\"country\":\"Malaysia\",\"push\":20543,\"click\":752,\"ctr\":3.66,\"cpc\":0.03,\"conversion\":0,\"conversionrate\":0,\"cpa\":0,\"SOV%\":\"3.87\"}],\"2032769\":[{\"country\":\"India\",\"push\":10460,\"click\":0,\"ctr\":0,\"cpc\":0.001,\"conversion\":7,\"conversionrate\":0.07,\"cpa\":2.22,\"SOV%\":\"0.28\"}]}";);

var country = d["2030417"][0]["country"];
Community
  • 1
  • 1
Benjamin Soulier
  • 2,223
  • 1
  • 18
  • 30