如何使用c#读取带动态节点的Json字符串?

时间:2016-09-02 08:59:41

标签: c# json

如何使用c#读取带动态节点的Json字符串?我无法读取密钥中的密钥和子节点。

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

1 个答案:

答案 0 :(得分:1)

你可以:

使用dynamic object&动态调用属性:

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

如果需要,您还可以迭代不同的属性:

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"];