1

I have the following json response.

var loginJsonString= {
"odata.metadata": "http://xxxxxxxx:7048/DynamicsNAV90/OData/$metadata#vehicle",
"value": [{
    "Model": "",
    "Description": "",
    "ETag": "16;kF8BAAJ7AAAAAAA=6;3481190;"
}, {
    "Model": "8889",
    "Description": "",
    "ETag": "28;kF8BAAJ7BDgAOAA4ADkAAAAAAA==6;2568000;"
}, {
    "Model": "AAA",
    "Description": "bbbb",
    "ETag": "24;kF8BAAJ7/0EAQQBBAAAAAAA=6;3481740;"
}, {
    "Model": "BMW",
    "Description": "aa",
    "ETag": "24;kF8BAAJ7/0IATQBXAAAAAAA=6;2464790;"
}, {
    "Model": "MODEL1",
    "Description": "Model1 Description.",
    "ETag": "32;kF8BAAJ7/00ATwBEAEUATAAxAAAAAAA=6;3868770;"
}, {
    "Model": "MODEL2",
    "Description": "Model2 Description.",
    "ETag": "32;kF8BAAJ7/00ATwBEAEUATAAyAAAAAAA=6;3868870;"
}, {
    "Model": "MODEL4",
    "Description": "Model4 Description.",
    "ETag": "32;kF8BAAJ7/00ATwBEAEUATAA0AAAAAAA=6;3869770;"
}, {
    "Model": "MODEL5",
    "Description": "Model5 Description.",
    "ETag": "32;kF8BAAJ7/00ATwBEAEUATAA1AAAAAAA=6;3869870;"
}, {
    "Model": "MODEL6",
    "Description": "Model6 Description.",
    "ETag": "32;kF8BAAJ7/00ATwBEAEUATAA2AAAAAAA=6;3986650;"
}, {
    "Model": "MODEL7",
    "Description": "Model7 Description.",
    "ETag": "32;kF8BAAJ7/00ATwBEAEUATAA3AAAAAAA=6;3987710;"
}, {
    "Model": "SSS",
    "Description": "sss",
    "ETag": "24;kF8BAAJ7/1MAUwBTAAAAAAA=6;3481310;"
}, {
    "Model": "VEHICLE MODEL",
    "Description": "",
    "ETag": "52;kF8BAAJ7/1YARQBIAEkAQwBMAEUAIABNAE8ARABFAEwAAAAAAA==6;3851550;"
}, {
    "Model": "VITZ",
    "Description": "Car Type",
    "ETag": "28;kF8BAAJ7/1YASQBUAFoAAAAAAA==6;3481880;"
}, {
    "Model": "VITZ1",
    "Description": "Car Type",
    "ETag": "28;kF8BAAJ7/1YASQBUAFoAMQAAAAAA6;3482080;"
}, {
    "Model": "VITZ2",
    "Description": "Car Type",
    "ETag": "28;kF8BAAJ7/1YASQBUAFoAMgAAAAAA6;3483250;"
}, {
    "Model": "VITZ3",
    "Description": "Car Type",
    "ETag": "28;kF8BAAJ7/1YASQBUAFoAMwAAAAAA6;3483600;"
}]

}

I want to deserialize the above response as, eg: 'Model= AAA Description=bbbb'

Following things I tried. Created a model class.

public class Vehicle
    {
        [JsonProperty("Model")]
        public string Model { get; set; }
        [JsonProperty("Description")]
        public string Description { get; set; }
        [JsonProperty("ETag")]
        public string ETag { get; set; }
    }

Then I used DeserializeObject() method.

Vehicle vehicleobj= JsonConvert.DeserializeObject<Vehicle>(loginJsonString);

But I cannot get values for vehicleobj. It shows null. Can anyone help me to solve this.

Matt Burland
  • 44,552
  • 18
  • 99
  • 171
RMD
  • 311
  • 1
  • 7
  • 22
  • The JSON is very different than your c# class. Please refer [this](https://stackoverflow.com/a/42708158/4228458) answer to create the correct c# class easily. – CodingYoshi Mar 02 '18 at 03:57

4 Answers4

2

You'll want this:

class LoginResponse
{

    [JsonProperty("odata.metadata")]
    public String ODataMetadata { get; set; }

    [JsonProperty("value")]
    public List<Vehicle> Vehicles { get; set; }
}

class Vehicle
{
    // same as before...
}

LoginResponse response = JsonConvert.DeserializeObject<Vehicle>( loginJsonString );
foreach( Vehicle veh in response.Vehicles )
{

}
Dai
  • 141,631
  • 28
  • 261
  • 374
1

Your problem is that you have a property value which is an array of Vehicle. So you'd need to deserialize to a class with that:

public class Response
{
     public List<Vehicle> Value {get; set;}
}

And then you can

var response = JsonConvert.DeserializeObject<Response>(loginJsonString);

And to get a vehicle you would do

var aVehicle = response.Value.FirstOrDefault();  // for example
Matt Burland
  • 44,552
  • 18
  • 99
  • 171
1

You are trying to deserialise the JSON object as a single Vehicle object, which it is not. It's something else, like perhaps a VehicleResponse:

New model (in addition to existing Vehicle class):

public class VehicleResponse { // I'm sure you can come up with a more appropriate name
{
    [JsonProperty("odata.metadata")]
    public string Metadata { get; set; }
    [JsonProperty("value")]
    public IEnumerable<Vehicle> Vehicles { get; set; }
}

Then deserialise as follows:

VehicleResult vehicleobj = JsonConvert.DeserializeObject<VehicleResult>(loginJsonString);
var vehicle1 = VehicleResult.Vehicles.FirstOrDefault(); // null if no vehicles
pcdev
  • 2,852
  • 2
  • 23
  • 39
0

Install Newtonsoft.json

public class Vehicle
{
    public class Value
    {
        public string Model { get; set; }
        public string Description { get; set; }
        public string ETag { get; set; }
    }

    public class RootObject
    {
        public List<Value> Value { get; set; }
    }
}

DeserializeObject

public async Task<RootObject> GetVehiclesAsync()
    {
        RootObject rootObject = null;
        var client = new HttpClient();
        string restUrl = "https://www.foo.com/vehicle/api/";
        var uri = new Uri(restUrl);
        var response = await client.GetAsync(uri);
        if (response.IsSuccessStatusCode)
        {
            var content = await response.Content.ReadAsStringAsync();
            rootObject = JsonConvert.DeserializeObject<RootObject>(content);
        }
        return rootObject;
    }

RootObject Root = await GetVehiclesAsync();
foreach (var item in Root.Value)
{
     // Do something here
     // item.Model
     // item.Description
     // item.ETag
}
Sourcephy
  • 239
  • 4
  • 4