在Json中获取代币



我对这个JSON有问题,无法在JArray中获得第二级:

{
  "Level1": {
    "Level2": [{
      "id": "Chart",
      "Box": [{
        "id": "1",
        "value": "10"
       },{
        "id": "2",
        "value": "20"
       }]
    }]
  }
}

我想以这种方式完全获得类似Array的2级:

JArray contasdasd = _jsonMaster["Level1"]["Level2"] as JArray;

我得到:

{
  "id": "Chart",
  "Box": [{
    "id": "1",
    "value": "10"
  },{
    "id": "2",
    "value": "20"
  }]
}

我想要:

"Level2": [{
  "id": "Chart",
  "Box": [{
    "id": "1",
    "value": "10"
  },{
    "id": "2",
    "value": "20"
  }]
}]

有什么办法达到二级吗?

当然你只是做

var level1 = _jsonMaster["Level1"];

问题是你已经走得太远了。

您对"Level2": [...]感兴趣的对象本身不是JArray,而是JProperty。您可以通过搜索Level1的属性来获得它,但请记住,您现在将拥有JProperty而不是JArray。该结果的Value将是您在当前代码中得到的JArray

    JProperty contasdasd = _jsonMaster["Level1"].First(o => (o as JProperty).Name == "Level2") as JProperty;
    Console.WriteLine(contasdasd.ToString()); // Will be what you are looking for
    Console.WriteLine(contasdasd.GetType()); // Will return JProperty
    Console.WriteLine(contasdasd.Value.GetType()); // Will return JArray

相关内容

  • 没有找到相关文章

最新更新