0

I got a JSON data like below and the FlightSegment object type seems one of the Array and the other one is not.

When I create an object as FlightSegment[], i am getting this error :

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'WA_2_0_RS.FlightSegment[]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) 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.

What it should be as c# class?

{
    "FlightSegment": {
        "DepartureAirport": {
            "LocationCode": "ESB"
        },
        "Ticket": "eTicket",
        "ArrivalAirport": {
            "LocationCode": "ADB"
        },
        "DateChangeNbr": false,
        "StopQuantity": "0",
        "CodeshareInd": false,
        "Equipment": {
            "Value": "UNKNOWN_PLANE",
            "AirEquipType": "***"
        },
        "DepartureDateTime": "2020-12-22T20:10:00.000+03:00",
        "ArrivalDateTime": "2020-12-22T21:30:00.000+03:00",
        "FlightNumber": "****",
        "OperatingAirline": {
            "CompanyShortName": "**"
        },
        "JourneyDuration": "P0DT1H20M0.000S"
    }
}, {
    "FlightSegment": [{
            "DepartureAirport": {
                "LocationCode": "ESB"
            },
            "Ticket": "eTicket",
            "ArrivalAirport": {
                "LocationCode": "SAW"
            },
            "DateChangeNbr": false,
            "StopQuantity": "0",
            "GroundDuration": "P0DT0H45M0.000S",
            "CodeshareInd": false,
            "Equipment": {
                "Value": "A320-200",
                "AirEquipType": "320"
            },
            "DepartureDateTime": "2020-12-22T12:05:00.000+03:00",
            "ArrivalDateTime": "2020-12-22T13:10:00.000+03:00",
            "FlightNumber": "****",
            "OperatingAirline": {
                "CompanyShortName": "**"
            },
            "JourneyDuration": "P0DT1H5M0.000S"
        }, {
            "DepartureAirport": {
                "LocationCode": "SAW"
            },
            "Ticket": "eTicket",
            "ArrivalAirport": {
                "LocationCode": "ADB"
            },
            "DateChangeNbr": false,
            "StopQuantity": "0",
            "CodeshareInd": false,
            "Equipment": {
                "Value": "UNKNOWN_PLANE",
                "AirEquipType": "73D"
            },
            "DepartureDateTime": "2020-12-22T13:55:00.000+03:00",
            "ArrivalDateTime": "2020-12-22T15:00:00.000+03:00",
            "FlightNumber": "****",
            "OperatingAirline": {
                "CompanyShortName": "**"
            },
            "JourneyDuration": "P0DT1H5M0.000S"
        }
    ]
}
Useme Alehosaini
  • 2,998
  • 6
  • 18
  • 26
  • 2
    You can try https://json2csharp.com/ just paste your json then get class for parsing the data. – Ahmed Ali Dec 22 '20 at 09:24
  • 1
    That is not a valid JSON document, so you'll not likely be able to parse it with any JSON serializer. You'll probably need to read it manually, if you cannot change the JSON structure – Camilo Terevinto Dec 22 '20 at 09:26
  • yes i did, but it creates FlightSegment as an "object". But object is also known type FlightSegment. I am asking that when i use it as FlightSegment and FlightSegment[] or List then it returns deserialize error like my first comment. – Aydin Gundogdu Dec 22 '20 at 09:27
  • 2
    It comes up often as a question, of how to parse json that is [sometimes one thing and sometimes another](https://stackoverflow.com/questions/5224697/deserializing-json-when-sometimes-array-and-sometimes-object) – Caius Jard Dec 22 '20 at 09:28
  • @CamiloTerevinto yes also i am thinking like you and asked source company to change their data as valid. But i don't believe that they will not change their Json structure. – Aydin Gundogdu Dec 22 '20 at 09:29
  • @CaiusJard Yeah, something like that could be applied here, but the JSON structure needs to be fixed first (right now there are 2 top-level objects which isn't valid) – Camilo Terevinto Dec 22 '20 at 09:32
  • You may look at this question [How to deserialize JSON with duplicate property names in the same object](https://stackoverflow.com/questions/20714160/how-to-deserialize-json-with-duplicate-property-names-in-the-same-object) – Pavel Anikhouski Dec 22 '20 at 09:33
  • @Ibnelaiq Well, try it with the JSON provided, you'll see that it cannot generate the correct structure. Please refrain from adding comments like that if you haven't validated whether the JSON can be parsed in the first place – Camilo Terevinto Dec 22 '20 at 09:34
  • @CamiloTerevinto whenever I see that (2+ top level obj) I assume that someone has accidentally stripped off the `[ ]` they got back from the service. Worth calling out tho; at the very least some client side patching might be needed if they won't change the output – Caius Jard Dec 22 '20 at 10:50
  • You may declare your `FlightSegment` property as a `List` and use `SingleOrArrayConverter` from [How to handle both a single item and an array for the same property using JSON.net](https://stackoverflow.com/q/18994685/3744182). In fact I think your question is a duplicate, agree? – dbc Dec 22 '20 at 16:23

1 Answers1

0

@Caius Jard's solution has fixed my issue.

Solution