【问题标题】:Json to c# get child nodesJson to c#获取子节点
【发布时间】:2014-10-31 17:40:05
【问题描述】:

我收到来自返回以下 json 的 API 的响应:

[
  {
   "input_index":0,
   "candidate_index":0,
   "components":,
      {
       "child1":"sam",
       "child2":"tom",
      }
  }
 ]

我需要使用 c# 和 json.net 获取 child1 和 child2 的结果。

【问题讨论】:

  • 太棒了 - 这听起来相当简单......那么到目前为止你尝试了什么?您是要反序列化到一个类,还是使用 LINQ to JSON? (JObject.Parse 等)?

标签: c# asp.net json


【解决方案1】:

应该相当简单。只需解析 JSON,然后从数组第一个索引的 components 对象中获取子项:

var obj = JArray.Parse(json);
string child1 = (string)obj[0]["components"]["child1"];
string child2 = (string)obj[0]["components"]["child2"];

【讨论】:

  • 这看起来像 NewtonSoft,而不是现代内置
  • @DavidV.Corbin 问题是询问如何使用 Newtonsoft (json.net) 进行操作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-10
  • 1970-01-01
相关资源
最近更新 更多