在c#中反序列化json响应

时间:2018-03-02 03:50:19

标签: c# asp.net-mvc json-deserialization

我有以下json回复。

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

}

我想将上面的回复反序列化为, 例如:'模型= AAA描述= bbbb'

我尝试过的事情。创建了一个模型类。

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

然后我使用了DeserializeObject()方法。

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

但我无法获得vehicleobj的价值。它显示为null。 任何人都可以帮我解决这个问题。

4 个答案:

答案 0 :(得分:1)

您的问题是您拥有的属性valueVehicle的数组。所以你需要反序列化到一个类:

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

然后你可以

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

要获得一辆车你会做

var aVehicle = response.Value.FirstOrDefault();  // for example

答案 1 :(得分:1)

你会想要这个:

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

}

答案 2 :(得分:1)

您正在尝试将JSON对象反序列化为单个Vehicle对象,而不是。这是其他的东西,比如VehicleResponse

新模型(除了现有的Vehicle类):

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

然后反序列表如下:

VehicleResult vehicleobj = JsonConvert.DeserializeObject<VehicleResult>(loginJsonString);
var vehicle1 = VehicleResult.Vehicles.FirstOrDefault(); // null if no vehicles

答案 3 :(得分:0)

安装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
}