反序列化JSON不能与newtonsoft.json

时间:2017-11-12 11:28:53

标签: c# json serialization json.net

我有一个json String如下所示,我使用反序列化到我的类对象但我不能,这是我的json。:

{
    "TraceId": "24bf6a01-5d8f-4959-9173-20600a04b738",
    "TransactionId": "AFC48AE50A076477C3E069296AC3F884",
    "ResponseTime": "1672",
    "DistanceUnits": "MI",
    "CurrencyType": "GBP",
    "xmlns:air": "http://www.travelport.com/schema/air_v42_0",
    "air:FlightDetailsList": {
            "air:FlightDetails": [
                {
                    "Key": "hx5kk+3R2BKAuFzqAAAAAA==",
                    "Origin": "DXB",
                    "Destination": "LHE",
                    "DepartureTime": "2017-12-10T13:55:00.000+04:00",
                    "ArrivalTime": "2017-12-10T17:55:00.000+05:00",
                    "FlightTime": "180",
                    "TravelTime": "180",
                    "Equipment": "320",
                    "OriginTerminal": "1",
                    "DestinationTerminal": "M"
                },
                {
                    "Key": "hx5kk+3R2BKAwFzqAAAAAA==",
                    "Origin": "LHE",
                    "Destination": "DEL",
                    "DepartureTime": "2017-12-20T12:15:00.000+05:00",
                    "ArrivalTime": "2017-12-20T14:10:00.000+05:30",
                    "FlightTime": "85",
                    "TravelTime": "690",
                    "Equipment": "ATR",
                    "OriginTerminal": "M",
                    "DestinationTerminal": "3"
                },
                {
                    "Key": "hx5kk+3R2BKAyFzqAAAAAA==",
                    "Origin": "DEL",
                    "Destination": "DXB",
                    "DepartureTime": "2017-12-20T20:25:00.000+05:30",
                    "ArrivalTime": "2017-12-20T22:45:00.000+04:00",
                    "FlightTime": "230",
                    "TravelTime": "690",
                    "Equipment": "788",
                    "OriginTerminal": "3",
                    "DestinationTerminal": "1"
                }
            ]
        }
}

所以我想转换成我的类 LowFareSearchRsp ,它的FlightDetails类也显示如下:

public class LowFareSearchRsp
{
    public string TraceId { get; set; }
    public string TransactionId { get; set; }
    public string ResponseTime { get; set; }
    public string DistanceUnits { get; set; }
    public string CurrencyType { get; set; }
    public string air { get; set; }
    public FlightDetailsList FlightDetailsList { get; set; }
}

public class FlightDetailsList
{
    public List<FlightDetails> FlightDetails { get; set; }
}

public class FlightDetails
{
    public string Key { get; set; }
    public string Origin { get; set; }
    public string Destination { get; set; }
    public DateTime Departure { get; set; }
    public DateTime ArrivalTime { get; set; }
    public string FlightTime { get; set; }
    public string TravelTime { get; set; }
    public string Equipment { get; set; }
    public string OriginTerminal { get; set; }
    public string DestinationTerminal { get; set; }
}

我正在使用NewtonSoft库将json反序列化为我的类对象,但收到以下错误:

  

{“无法反序列化当前的JSON对象(例如   {\“name \”:\“value \”})进入类型   'System.Collections.Generic.List`1 [ParseSoapEnveloperReqRes.LowFareSearchRsp]'   因为该类型需要一个JSON数组(例如[1,2,3])来反序列化   正确。\ r \ n要修复此错误,请将JSON更改为JSON数组   (例如[1,2,3])或更改反序列化类型以使其正常   .NET类型(例如,不是整数的基本类型,不是集合   类似于数组或List的类型,可以从JSON反序列化   宾语。 JsonObjectAttribute也可以添加到类型中以强制它   从JSON对象反序列化。\ r \ n''TraceId',第2行,位置   12。“}

这是我的反序列化json字符串的代码:

var LowFareSearchRsps = JsonConvert.DeserializeObject<List<LowFareSearchRsp>>(jsonString);

我曾搜索过整个互联网,但无法找到解决方法。请帮助。感谢。

2 个答案:

答案 0 :(得分:2)

您的JSON不代表List<LowFareSearchRsp>,而是代表LowFareSearchRsp

答案 1 :(得分:2)

您的JSON字符串表示一个对象,而不是数组更改deserilize代码,如下所示:

var LowFareSearchRsps = JsonConvert.DeserializeObject<LowFareSearchRsp>(jsonString);