【问题标题】:Given a JSON value, how do I get the parent's sibling value?给定一个 JSON 值,我如何获取父级的兄弟值?
【发布时间】:2020-04-25 12:58:10
【问题描述】:

我有一个 .NET Core 3.1 C# 应用程序读取以下 JSON 文档:

{
  "info": {
    "_postman_id": "b"
  },
  "item": [
    {
      "name": "GetEntityById via APIM",
      "item": [
        {
          "name": "Call 1",
          "url": {
            "raw": "urlforcall1"
          }
        },
        {
          "name": "Call 2",
          "url": {
            "raw": "urlforcall2"
          }
        }
      ]
    }
  ]
}

我想为每个item\item\name 和每个item\item\url\raw 选择值。

所以,我想以"Call 1":"urlforcall1" 和"Call 2":"urlforcall2" 结束。

我一直在玩,可以通过以下方式从raw 令牌中获取值:

var jObject = JObject.Parse(jsonString);
var urls = jObject.SelectTokens("..raw");

如何从其父级的兄弟name 获取值?

【问题讨论】:

  • 您不想将 JSON 序列化为 C# 对象吗?

标签: c# json .net-core json.net


【解决方案1】:

希望这段代码对你有帮助

using Newtonsoft.Json.Linq;
using System;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            string json = @"
            {
                'info': {
                     '_postman_id': 'b'
                },
                'item': [
                {
                     'name': 'GetEntityById via APIM',
                     'item': [
                     {
                          'name': 'Call 1',
                          'url': {
                          'raw': 'urlforcall1',
                     }
                 },
                 {
                     'name': 'Call 2',
                     'url': {
                     'raw': 'urlforcall2',
                 }
             }
         ]
     }
 ]
            }";

            dynamic d = JObject.Parse(json);

            foreach(var item in d.item)
            {
                foreach(var innerItem in item.item)
                {
                    Console.WriteLine($"'{innerItem.name}' : '{innerItem.url.raw}'");
                }
            }
        }
    }
}

可以在这里测试https://dotnetfiddle.net/xDr90O

【讨论】:

    【解决方案2】:

    要直接回答您的问题,如果您有JToken,您可以使用Parent 属性从那里向上导航。在你的情况下,你需要使用它四次才能达到你想要的水平:

    • 表示调用 URL 字符串的 JValue 的父级是名称为 raw 的 JProperty
    • JProperty 的父级是 JObject
    • JObject 的父级是 JProperty,名称为 url
    • JProperty 的父级是 JObject,其中还包含 name 属性

    从那里您可以使用索引器语法向下导航以获取 name 的值。

    所以,你会得到这样的结果:

    var jObject = JObject.Parse(jsonString);
    
    foreach (JToken raw in jObject.SelectTokens("..raw"))
    {
        string callName = (string)raw.Parent.Parent.Parent.Parent["name"];
        string urlForCall = (string)raw;
    }
    

    【讨论】:

      【解决方案3】:

      您可以使用SelectMany方法将内部item数组扁平化为一个序列(因为外部item也是一个数组),然后直接按键获取name和raw值

      var jObject = JObject.Parse(jsonString);
      
      var innerItems = jObject["item"]?.SelectMany(t => t["item"]);
      foreach (var item in innerItems)
      {
          var name = item["name"];
          var raw = item["url"]?["raw"];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-23
        相关资源
        最近更新 更多