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

时间:2016-03-25 04:54:09

标签: c# json json.net

我有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”和“value”的值,如下面的json格式

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

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

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

谢谢,

1 个答案:

答案 0 :(得分:2)

根据您在问题中显示的示例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