JoshuaRG1993 JoshuaRG1993 -4 years ago 43
C# Question

How can I extract these key/value pairs from this JSON node?

I've been successfully using JToken and JArray to grab specific pieces of data, but I'm having one particular issue. The issues lie with extracting the values from the key/value pairs in my node 'work_unit.' You can see the node here in my JObject:

JObject testString = JObject.Parse("{"
+ "'Roofing': {"
+ "'Buildings': ["
+ "{"
+ "'BuildingId': 4,"
+ "'BuildingName': 'what',"
+ "'work_unit': '{\"RoofingBuildings\":\"1\",\"WindowsBuildings\":\"\",\"GutterBuildings\":\"\",\"InsulationMasterPrice\":\"\",\"SidingBuildings\":\"\"}'"
+ "},"
+ "{"
+ "'BuildingId': 3,"
+ "'BuildingName': 'Home'"
+ "}"
+ "],"

+ "'Windows': ["
+ "{"
+ "'PerimeterDrawings': 56,"
+ "},"
+ "{"
+ "'PerimeterDrawings': 55,"
+ "}"
+ "]"

+ "},"

+ "'Window':"
+ "{"
+ "'poof': 3,"
+ "'stall': 7"
+ "},"

+ "'Products': ["
+ "'Roofing',"
+ "'Pooping',"
+ "],"

+ "'Garage': ["
+ "{"
+ "'roof': 3,"
+ "'wall': 7"
+ "},"
+ "{"
+ "'roof': 3,"
+ "'wall': 7"
+ "}"
+ "]"
+ "}");


I can get the entirety of 'work_unit' like so:

JToken jt= JO.SelectToken("Roofing.Buildings[0].work_unit");
^^ jt will contain:
{{"RoofingBuildings":"1","WindowsBuildings":"","GutterBuildings":"","InsulationMasterPrice":"","SidingBuildings":""}}


How do I get the "1" from RoofingBuildings? I've tried several methods, here are some, but they just end up being null.

string foo1 = (string)JTNewTest.SelectToken(".RoofingBuildings");
string foo2 = (string)JO.SelectToken("Roofing.Buildings[0].work_unit[0].RoofingBuildings");


Thanks in advance, I couldn't find my exact situation in any other online examples for some reason :/

Answer Source

You can get it like this, I assumed that the work_unit was supposed to be split up into it's own properties rather than being a string.. Hopefully I was correct in that assumption. Also made multi-line string for readability sake.

    JObject testString = JObject.Parse(@"
    {
        'Roofing': {
            'Buildings': 
            [
                {
                    'BuildingId': 4,
                    'BuildingName': 'what',
                    'work_unit': 
                    {
                        'RoofingBuildings':'1',
                        'WindowsBuildings':'',
                        'GutterBuildings':'',
                        'InsulationMasterPrice':'',
                        'SidingBuildings':''
                    }
                },
                {
                    'BuildingId': 3,
                    'BuildingName': 'Home'
                }
            ],
            'Windows': 
            [
                {
                    'PerimeterDrawings': 56,
                },
                {
                    'PerimeterDrawings': 55,
                }
            ]
        },
        'Window':
        {
            'poof': 3,
            'stall': 7
        },
        'Products': 
        [
            'Roofing',
            'Pooping'
        ],
        'Garage': 
        [
            {
                'roof': 3,
                'wall': 7
            },
            {
            'roof': 3,
            'wall': 7
            }
        ]
    }");

    var roofingBuildings = testString["Roofing"]["Buildings"][0]["work_unit"]["RoofingBuildings"];

    Console.WriteLine("RoofingBuildings: {0}", roofingBuildings);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download