嵌套Json-如何访问项目?

时间:2017-09-13 11:24:09

标签: c# json string parsing object

我有一个像这样的jsonString:

<ul class="list user_list">
  <li>
    <a href="BT1672" title="Test Event" class="sl_name">
      Test Event<br>
    </a>
  </li>
  <small class="s_color sl_email">29-09-2017 12:50 PM</small>
</ul>

所以我的 { "total": 0, "subtotal": 88, "page": 1, "per_page": 100, "search": "records", "sort": { "by": null, "order": null }, "results": { "x-name": { "records": "[{\"instance\":\"devsupport\",\"details\":[{\"id\":\"2\",\"hostname\":\"a\",\"ip\":\"i\",\"macaddr\":\"m\",\"user_created\":\"system\",\"date_created\":\"2015-07-10 11:45:20\",\"date_last_update\":null}]" }, "y-name": { "records": "[{\"instance\":\"devsupport\",\"details\":[{\"id\":\"2\",\"hostname\":\"a\",\"ip\":\"i\",\"macaddr\":\"m\",\"user_created\":\"system\",\"date_created\":\"2015-07-10 11:45:20\",\"date_last_update\":null}]" } } } 就像这样创建并正确显示:

jo object

我的主要问题是我如何获得Object jo = JObject.Parse(jsonString); r等等,并将它们列入一个列表,考虑到 “结果”中的记录具有所有不同的名称,如hostname,ip,macadd等。

1 个答案:

答案 0 :(得分:0)

你必须改变你的Json或再次解析records内的json,没有其他直接的方式,如object->results->xname->records->ip

你可以这样做

recordsjson = object->results->xname->records
records = JObject.Parse(recordsjson);
ip = records->ip

或者

像这样改变你的records结构

"records": [
    {
      "instance": "devsupport",
      "details": [
        {
          "id": "2",
          "hostname": "a",
          "ip": "i",
          "macaddr": "m",
          "user_created": "system",
          "date_created": "2015-07-10 11:45:20",
          "date_last_update": null
        }
      ]
    }
  ]