【问题标题】:Parse JSON data with JSON.NET使用 JSON.NET 解析 JSON 数据
【发布时间】:2015-01-10 01:38:19
【问题描述】:

有很多关于此的问题,但我找不到解决问题的方法。

我的 JSON 看起来像这样:

{
"index":[
  {
     "Color":"Blue",
     "URL":"SomeURL",

     "Persons":[
        {
           "name":"Charlie",
           "Country":"Denmark",
           "Security number":"25663456"
        }
     ],

     "Color":"Green",
     "URL":"SomeURL",

     "Persons":[
        {
           "name":"Putin",
           "Country":"Russia",
           "Security number":"78495832"
         }
       ],
    ],
  } 
 "total":"2"
}

我可以访问的唯一 JSON 数据是 indextotal

如何仅访问和打印nameCountryColor

【问题讨论】:

  • 索引是一个数组。 index[0].Color 会给你“蓝色”等...
  • 仅供参考,这不是有效的 JSON;你在最后一个数组之前关闭一个块
  • @DrewMcGowen 很抱歉,因为我是通过查看写下来的,可能会有一些错误
  • @peer,谢谢!做到了!
  • "因为我是看写的" 你是手写的吗?为什么不复制粘贴?

标签: c# json.net


【解决方案1】:

index 是一个数组。 index[0].Color 会给你“蓝色”等...

【讨论】:

    【解决方案2】:

    索引是一个对象数组。要访问它,您必须遍历它,或者通过数组中的索引访问每个元素。然后,您将可以访问您在提要中为其设置的属性。

    如果你使用 JSON.Net 库,你可以这样做:

    dynamic jsonObj = JsonConvert.DeserializeObject<dynamic>(target)
    foreach(var item in jsonObj.index)
    {
        string color = item.Color;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-21
      • 2010-09-28
      • 1970-01-01
      • 1970-01-01
      • 2014-03-05
      • 2014-11-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多