0

I have Jobject for example:

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

and i want to get the value of "alias" and "value" as below json format

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

so, is there any built in method in json.net where i can get the specific property value?

or do I need to implement some recursive method in c# which will search JToken by name in all JTokens and JArrays in JObject? any suggestion?

Thanks,

Harshit
  • 397
  • 2
  • 6
  • 17
  • Have you tried this [reference](http://stackoverflow.com/questions/19645501/searching-for-a-specific-jtoken-by-name-in-a-jobject-hierarchy?rq=1) ? – Nid Mar 25 '16 at 06:05
  • yes, i was checking it however my result set is a bit different where value of one property "alias " which is "state " is the property for "value " which is "test" ... sample code is: 'code' "id": 0, "value": "test", "alias": "state", "editor": "Umbraco.Textbox", "visible": "true" }, – Harshit Mar 25 '16 at 06:16

1 Answers1

2

Based on the sample JSON you've shown in your question, you should be able to get the result you want like this:

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

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
  • Thanks a lot @brain-rogers :) can we loop it through when we have more then 1 levels ?i just changed the json which is having 3 levels. fiddle: dotnetfiddle.net/XsHiUG – Harshit Mar 25 '16 at 07:03