简体   繁体   中英

Deserialize JSON not working with newtonsoft.json

I have a json String as below, which i am using the deserialize into my class objects but I can't, here is my 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"
                }
            ]
        }
}

So I want to convert into my class LowFareSearchRsp and its FlightDetails class is also shown below:

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

and I'm using the NewtonSoft library to deserialize that json to my class object but getting the following error:

{"Cannot deserialize the current JSON object (eg {\\"name\\":\\"value\\"}) into type 'System.Collections.Generic.List`1[ParseSoapEnveloperReqRes.LowFareSearchRsp]' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly.\\r\\nTo fix this error either change the JSON to a JSON array (eg [1,2,3]) or change the deserialized type so that it is a normal .NET type (eg not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.\\r\\nPath 'TraceId', line 2, position 12."}

Here is my code to deserialize the json string:

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

I'he had searched the whole internet, but can't find the solution of this. Kindly Help. Thanks.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM