在JObject层次结构中搜索特定的JToken,并将它们存储在另一个JObject中



我有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""
                }
              ]
            }
          ]
        }";

我想得到"alias"one_answers"value"的值,如下json格式

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

那么,在json.net中有没有内置的方法可以让我获得特定的属性值呢?

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

谢谢,

基于您在问题中显示的示例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"]))));

Fiddle:https://dotnetfiddle.net/DOfKaJ

相关内容

  • 没有找到相关文章

最新更新