如何从JSON数据

时间:2018-05-21 00:56:24

标签: c# json winforms json.net treeview

我有一个像这样的JSON文件内容:

{
    "cars": [
        {
            "code": "MB-GLS",
            "name": "Mercedes-Benz GLS 350 d 4MATIC",
            "stock": {
                "AMG 9G-Tronic 2016 Blue",
                "4M AMG 2017 Black"
            }
        },
        {
            "code": "BM-420D",
            "name": "BMW 420d Cabrio",
            "stock": {
                "420d Cabrio Luxury Line 2015 White",
                "M Sport 420d Cabrio Red 2018",
                "420d Cabrio Premium 2014 Black",
                "420d Cabrio Premium 2015 White"
            }
        },
        {
            "code": "AU-A5",
            "name": "A5 Sportback 2.0 TDI quattro",
            "stock": {
                "2.0 TDI quattro Dynamic 2018 Grey",
                "2.0 TDI quattro S-Line 2018 Black",
                "2.0 TDI quattro S-Line 2018 Indigo",
                "2.0 TDI quattro Design 2017 White",
                "2.0 TDI quattro Design 2016 Black"
            }
        },
        {
            "code": "TS-MOS",
            "name": "Tesla Model S",
            "stock": {
                "Model S 75D 525 PS 2018 White",
                "Model S 75D 525 PS 2018 Indigo",
                "Model S 90D 2017 Red",
                "Model S P100D 775 PS 2018 Smoke-Colored",
                "Model S P100D 775 PS 2018 White",
                "Model S P85+ 2013 Blue"
            }
        }
    ]
}

我想在WinForms上从这个JSON数据创建一个树视图内容。我也希望使用treeView1控件。最后我使用JSON.Net创建JSON对象。

我希望结果如下:TreeView content with created photoshop for example

我不知道,我该怎么办呢。

编辑:当我尝试Creating tree view dynamically according to json text in Winforms举例时,会出现如下错误:Invalid character after parsing property name. Exprected ':' but got: ,. Path 'cars[0].stock', line 7, position 29.。它给出了,因为它想要这样的股票:

   "stock": {
        "1": "420d Cabrio Luxury Line 2015 White",
        "2": "M Sport 420d Cabrio Red 2018",
        "3": "420d Cabrio Premium 2014 Black",
        "4": "420d Cabrio Premium 2015 White"
    }

但我们的股票不喜欢这个。

最后,当我尝试How to recursively populate a TreeView with JSON data 时,它会出现如下错误:Illegal characters in path', line 7, position 29.。我查看了json数据,你也看到,JSON数据中没有错误。

0 个答案:

没有答案