如何从此JSON节点中提取这些键/值对?

时间:2017-05-19 18:27:52

标签: c# json json.net

我已成功使用JToken和JArray获取特定数据,但我遇到了一个特定问题。问题在于从我的节点' work_unit中的键/值对中提取值。'你可以在我的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"
                 + "}"
                + "]"
        + "}");

我可以获得整个' work_unit'像这样:

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

如何获得" 1"来自RoofingBuildings?我尝试了几种方法,这里有一些,但它们最终都是空的。

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

在此先感谢,由于某些原因,我无法在任何其他在线示例中找到我的确切情况:/

1 个答案:

答案 0 :(得分:0)

你可以这样得到它,我假设work_unit应该被分成它自己的属性而不是一个字符串..希望我在那个假设中是正确的。为了便于阅读,还制作了多行字符串。

    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);