1

Here is my json data file content:

{
    "mercedes": {
        "model" : "GLS 350 d 4MATIC",
        "code" : "MB-GLS",
        "year": 2015
    },
    "bmw": {
        "model" : "BMW 420d Cabrio",
        "code" : "BM-420D",
        "year": 2017
    },
    "audi": {
        "model" : "A5 Sportback 2.0 TDI quattro",
        "code" : "AU-A5",
        "year": 2018
    },
    "tesla": {
        "model" : "Model S",
        "code" : "TS-MOS",
        "year": 2016
    },
  "test_drive": 0,
  "path": "D:\\Mizz\\cars\\"
}

Firstly, is my json structure right? Then I want to get "model" properties as a list. But I have no idea about this.

  • 1
    You know that you need a class that represent this JSON object don't you? So that's where you need to start.. :) https://stackoverflow.com/questions/24496941/deserialisation-of-a-nested-json-object-with-newtonsoft-in-c-sharp?rq=1 – Romeo Sierra May 12 '18 at 17:32
  • 1
    @RomeoSierra: That's actually not the best approach here, since he wants the different properties in a list rather than a static model. – StriplingWarrior May 12 '18 at 17:48
  • 1
    Looks like your root object has a mixture of fixed properties (`"test_drive"` and `"path"`) along with a variable set of properties (car names) whose values have a fixed schema. If so, you can use `[JsonTypedExtensionData] public Dictionary { get; set; }` from [How to deserialize a child object with dynamic (numeric) key names?](https://stackoverflow.com/a/40094403/3744182). – dbc May 12 '18 at 17:48
  • Your list of car types should probably be in an array instead of as object properties. Starting with a poor json design is going to cause you massive headaches – Joe Phillips May 12 '18 at 20:02
  • @Habip Can you change the data structure? If so, let's start there – Joe Phillips May 12 '18 at 20:05

2 Answers2

2

You are gonna need to create a class for the car structure with a model, code and year property :

public class Car {
   public string model;
   public string code;
   public string year;
}

Then with JsonConvert, you can use the Deserialize<T>(string json) method like this :

var cars = JsonConvert.Deserialize<Dictionary<string, Car>>(json);

Afterwards, if you want only the car models dumped into a list, you can use the Map function on the values of the car dictionary :

var models = cars.Values.Map(car => car.model);

P.S : I wrote this in a raw text editor, it might not be the right syntax, but you get the idea. This could apply also in a couple of other languages.

CBinet
  • 187
  • 2
  • 12
  • 1
    This is the right approach but he doesn't have a list of cars. His JSON structure probably should be changed before bothering to help write the deserialization steps – Joe Phillips May 12 '18 at 20:05
  • dictionary.Values will return a list of cars. dictionary.Keys would return the list ```["mercedes", "bmw", "audi", "tesla"]```. But I agree that a better structure would be the right approach. – CBinet May 12 '18 at 20:39
  • 1
    a dictionary is a list of key value pairs though... he doesn't have a list. I'd be surprised if newtosoft can interpret what he has as a dictionary. Also, this would fail once it hits the last 2 properties that arent cars – Joe Phillips May 12 '18 at 20:51
2

If you can change your data structure, that would be ideal. The one you have now will not scale well.

{
    "vehicles": [
        {
            "mfgr": "mercedes",
            "model" : "GLS 350 d 4MATIC",
            "code" : "MB-GLS",
            "year": 2015
        },
        {
            "mfgr": "bmw",
            "model" : "BMW 420d Cabrio",
            "code" : "BM-420D",
            "year": 2017
        },
        {
            "mfgr": "audi",
            "model" : "A5 Sportback 2.0 TDI quattro",
            "code" : "AU-A5",
            "year": 2018
        },
        {
            "mfgr": "tesla",
            "model" : "Model S",
            "code" : "TS-MOS",
            "year": 2016
        }
    ],
  "testDrive": 0,
  "path": "D:\\Mizz\\cars\\"
}

Then you need to model a "Car" type in C#.

public class Car
{
   public string Mfgr { get; set; }
   public string Model { get; set; }
   public string Code { get; set; }
   public int Year { get; set; }
}

And the outer object type as well.

public class MyOuterType
{
    public List<Car> Vehicles { get; set; }
    public int TestDrive { get; set; }
    public string Path { get; set; }
}

Then you can use JsonConvert.Deserialize<MyOuterType>(theJson); to get your object.

Joe Phillips
  • 49,743
  • 32
  • 103
  • 159