3

I have a JSON file content like this:

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

I want to create a treeview content from this JSON data on the WinForms. And I want to use treeView1 control for it too. Lastly I am using JSON.Net for create JSON object.

The result I hope is the following: TreeView content with created photoshop for example

I don't any idea, how can I manage this.

EDIT : When I try Creating tree view dynamically according to json text in Winforms subjected example, it gives an error like this: Invalid character after parsing property name. Exprected ':' but got: ,. Path 'cars[0].stock', line 7, position 29.. It gives, because it wants stock like this:

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

But our stock does not like this.

And finally when I try How to recursively populate a TreeView with JSON data it gives an error like this: Illegal characters in path', line 7, position 29.. I checked json data, you also see, there is no error in the JSON data.

Jack Eagle
  • 31
  • 1
  • 5
  • You might want to add a tag so we know which UI API you're using. – ProgrammingLlama May 21 '18 at 01:02
  • Is it a desktop or web application where in you would need a tree view – Maddy May 21 '18 at 01:05
  • @john, I don't know, do I have to use JSON.Net. Think like that: `string json = "{\n\"cars\":[\";...` – Jack Eagle May 21 '18 at 01:15
  • Take a look at [How to recursively populate a TreeView with JSON data](https://stackoverflow.com/q/39673815/3744182). Does that answer your question? – dbc May 21 '18 at 02:11
  • I felt I needed to open up a new topic because I could not find the answer to the other question. – Jack Eagle May 21 '18 at 10:06
  • @JackEagle - your JSON is not well-formed. Upload it to https://jsonlint.com/ and you will get an error, `Error: Parse error on line 6: Expecting ':', got ','`. The problem is that you have a property `"stock": { "string", "string" }`. That looks like an array of strings, but as explained at http://www.json.org/ an array must be surrounded by brackets `[` and `]`, not braces. So you will need to fix your JSON to be well-formed in order to parse it with any JSON parser. – dbc May 21 '18 at 16:37
  • @dbc - Ok! Thank you very much. I will try as you say. – Jack Eagle May 22 '18 at 11:54

0 Answers0