【问题标题】:Searching for specific JToken in JObject hierarchy and store them in another JObject在 JObject 层次结构中搜索特定的 JToken 并将它们存储在另一个 JObject 中
【发布时间】:2016-07-12 20:56:06
【问题描述】:

例如,我有 Jobject:

string json = @"
        {
          ""memberdetails"": [
            {
              ""id"": 0,
              ""label"": ""General Details"",
              ""visible"": true,
              ""properties"": [
                {
                  ""label"": ""Address"",
                  ""description"": ""Residential or Postal Address"",
                  ""view"": ""textarea"",
                  ""config"": {},
                  ""hideLabel"": false,
                  ""validation"": {
                    ""mandatory"": false,
                    ""pattern"": null
                  },
                  ""id"": 0,
                  ""value"": ""test 1"",
                  ""alias"": ""address"",
                  ""editor"": ""Umbraco.TextboxMultiple"",
                  ""visible"": ""true""
                },
                {
                  ""label"": ""State"",
                  ""description"": ""State of residence"",
                  ""view"": ""textbox"",
                  ""config"": {},
                  ""hideLabel"": false,
                  ""validation"": {
                    ""mandatory"": false,
                    ""pattern"": null
                  },
                  ""id"": 0,
                  ""value"": ""test 2"",
                  ""alias"": ""state"",
                  ""editor"": ""Umbraco.Textbox"",
                  ""visible"": ""true""
                }
              ]
            },
            {
              ""id"": 1,
              ""label"": ""Other Details"",
              ""visible"": true,
              ""properties"": [
                {
                  ""label"": ""Address"",
                  ""description"": ""Residential or Postal Address"",
                  ""view"": ""textarea"",
                  ""config"": {},
                  ""hideLabel"": false,
                  ""validation"": {
                    ""mandatory"": false,
                    ""pattern"": null
                  },
                  ""id"": 0,
                  ""value"": ""test_otherdetails1 "",
                  ""alias"": ""address"",
                  ""editor"": ""Umbraco.TextboxMultiple"",
                  ""visible"": ""true""
                },
                {
                  ""label"": ""State"",
                  ""description"": ""State of residence"",
                  ""view"": ""textbox"",
                  ""config"": {},
                  ""hideLabel"": false,
                  ""validation"": {
                    ""mandatory"": false,
                    ""pattern"": null
                  },
                  ""id"": 0,
                  ""value"": ""test_otherdetails2"",
                  ""alias"": ""state"",
                  ""editor"": ""Umbraco.Textbox"",
                  ""visible"": ""true""
                }
              ]
            },
                {
                 ""id"": 2,
              ""label"": "" Details"",
              ""visible"": true,
              ""properties"": [
                {
                  ""label"": ""Address"",
                  ""description"": ""Residential or Postal Address"",
                  ""view"": ""textarea"",
                  ""config"": {},
                  ""hideLabel"": false,
                  ""validation"": {
                    ""mandatory"": false,
                    ""pattern"": null
                  },
                  ""id"": 0,
                  ""value"": "" Details1"",
                  ""alias"": ""address"",
                  ""editor"": ""Umbraco.TextboxMultiple"",
                  ""visible"": ""true""
                },
                {
                  ""label"": ""State"",
                  ""description"": ""State of residence"",
                  ""view"": ""textbox"",
                  ""config"": {},
                  ""hideLabel"": false,
                  ""validation"": {
                    ""mandatory"": false,
                    ""pattern"": null
                  },
                  ""id"": 0,
                  ""value"": ""Details1"",
                  ""alias"": ""state"",
                  ""editor"": ""Umbraco.Textbox"",
                  ""visible"": ""true""
                }
              ]
            }
          ]
        }";

我想得到如下json格式的“别名”和“值”的值

[
{"address": "test"},
{"state": "test"}
]

那么,json.net 中是否有任何内置方法可以让我获得特定的属性值?

或者我是否需要在 c# 中实现一些递归方法,该方法将在 JObject 中的所有 JToken 和 JArray 中按名称搜索 JToken? 有什么建议吗?

谢谢,

【问题讨论】:

  • 你试过这个reference 吗?
  • 是的,我正在检查它但是我的结果集有点不同,其中一个属性“alias”的值是“state”是“value”的属性,即“test”......示例代码是:'code'“id”:0,“value”:“test”,“alias”:“state”,“editor”:“Umbraco.Textbox”,“visible”:“true”},跨度>

标签: c# json json.net


【解决方案1】:

根据您在问题中显示的示例 JSON,您应该能够得到您想要的结果,如下所示:

JObject obj = JObject.Parse(json);

JArray result = new JArray(
    obj.SelectToken("memberdetails[0].properties")
       .Select(jt => new JObject(new JProperty((string)jt["alias"],jt["value"]))));

小提琴:https://dotnetfiddle.net/DOfKaJ

【讨论】:

  • 非常感谢@brain-rogers :) 当我们有超过 1 个级别时我们可以循环它吗?我只是更改了具有 3 个级别的 json。小提琴:dotnetfiddle.net/XsHiUG
猜你喜欢
  • 2013-11-07
  • 1970-01-01
  • 1970-01-01
  • 2020-05-15
  • 1970-01-01
  • 2021-03-14
  • 2014-07-26
  • 1970-01-01
  • 2016-11-28
相关资源
最近更新 更多