1

I am trying to generate classes from json to deseralize to c# object but list object is not correct, where am i going wrong? Available rooms should be a list as per my assumption but It does not work.

Below is my JSON

{
  "avaliabilitiesResponse": {
    "Hotels": {
      "Hotel": [
        {
          "HotelCode": "0017855",
          "HotelsName": "Hilton Jeddah",
          "Location": "Jeddah, Saudi Arabia",
          "Rating": "4",
          "LowestPrice": "35",
          "Currency": "EUR",
          "IsReady": "true",
          "AvailableRooms": {
            "AvailableRoom": [
              {
                "RoomCode": "11245",
                "RoomName": "Standard",
                "Occupancy": "1",
                "Status": "true"
              },
              {
                "RoomCode": "12000",
                "RoomName": "Double",
                "Occupancy": "2",
                "Status": "true"
              },
              {
                "RoomCode": "99685",
                "RoomName": "Twin",
                "Occupancy": "2",
                "Status": "true"
              }
            ]
          }
        },
        {
          "HotelCode": "0018799",
          "HotelsName": "Ramada Continental Jeddah",
          "Location": "Jeddah, Saudi Arabia",
          "Rating": "3",
          "LowestPrice": "345",
          "Currency": "USD",
          "IsReady": "false",
          "AvailableRooms": {
            "AvailableRoom": [
              {
                "RoomCode": "00012",
                "RoomName": "Triple Standard",
                "Occupancy": "3",
                "Status": "false"
              },
              {
                "RoomCode": "5477",
                "RoomName": "Triple Sea View",
                "Occupancy": "3",
                "Status": "false"
              },
              {
                "RoomCode": "996666",
                "RoomName": "Standard Double",
                "Occupancy": "2",
                "Status": "true"
              }
            ]
          }
        },
        {
          "HotelCode": "0010888",
          "HotelsName": "Qasr Al Sharq",
          "Location": "Jeddah, Saudi Arabia",
          "Rating": "5",
          "LowestPrice": "3500",
          "Currency": "SAR",
          "IsReady": "true",
          "AvailableRooms": {
            "AvailableRoom": {
              "RoomCode": "102432",
              "RoomName": "Suite",
              "Occupancy": "4",
              "Price": "3500",
              "Status": "true"
            }
          }
        }
      ]
    }
  }
}

it gets converted to c# like this.

public class AvailableRooms
{
    public object AvailableRoom { get; set; } //this is not correct i assume, i tried to fix it like Hotels have Hotel in Json to have a list here but it does not work. 
}

public class Hotel
{
    public string HotelCode { get; set; }
    public string HotelsName { get; set; }
    public string Location { get; set; }
    public string Rating { get; set; }
    public string LowestPrice { get; set; }
    public string Currency { get; set; }
    public string IsReady { get; set; }
    public AvailableRooms AvailableRooms { get; set; }
}

public class Hotels
{
    public List<Hotel> Hotel { get; set; }
}

public class AvaliabilitiesResponse
{
    public Hotels Hotels { get; set; }
}

public class RootObject
{
    public AvaliabilitiesResponse avaliabilitiesResponse { get; set; }
}

I have tried using Json2csharp as well as visual studio's paste special option for class conversion, I still don't get available rooms in my object.

3
  • You shall post the error also Commented Apr 5, 2019 at 13:17
  • In c# I don't see my available object for Rooms. There is no error but I don't see the list of rooms against hotels. Commented Apr 5, 2019 at 13:19
  • Your json is wrong, hotels should be an array and not an object, and hotel should be every object of the hotels array Commented Apr 5, 2019 at 13:31

3 Answers 3

2

Here is fixed json (problem was the last AvailableRooms, which had an invalid member AvailableRoom which was both array and object). This JSON can be pasted json-as-classes with visual studio:

{
  "avaliabilitiesResponse": {
    "Hotels": {
      "Hotel": [
        {
          "HotelCode": "0017855",
          "HotelsName": "Hilton Jeddah",
          "Location": "Jeddah, Saudi Arabia",
          "Rating": "4",
          "LowestPrice": "35",
          "Currency": "EUR",
          "IsReady": "true",
          "AvailableRooms": 
            [
              {
                "RoomCode": "11245",
                "RoomName": "Standard",
                "Occupancy": "1",
                "Status": "true"
              },
              {
                "RoomCode": "12000",
                "RoomName": "Double",
                "Occupancy": "2",
                "Status": "true"
              },
              {
                "RoomCode": "99685",
                "RoomName": "Twin",
                "Occupancy": "2",
                "Status": "true"
              }
            ]
          }
        ,
        {
          "HotelCode": "0018799",
          "HotelsName": "Ramada Continental Jeddah",
          "Location": "Jeddah, Saudi Arabia",
          "Rating": "3",
          "LowestPrice": "345",
          "Currency": "USD",
          "IsReady": "false",
          "AvailableRooms" :  
            [
              {
                "RoomCode": "00012",
                "RoomName": "Triple Standard",
                "Occupancy": "3",
                "Status": "false"
              },
              {
                "RoomCode": "5477",
                "RoomName": "Triple Sea View",
                "Occupancy": "3",
                "Status": "false"
              },
              {
                "RoomCode": "996666",
                "RoomName": "Standard Double",
                "Occupancy": "2",
                "Status": "true"
              }
            ]

        },
        {
          "HotelCode": "0010888",
          "HotelsName": "Qasr Al Sharq",
          "Location": "Jeddah, Saudi Arabia",
          "Rating": "5",
          "LowestPrice": "3500",
          "Currency": "SAR",
          "IsReady": "true",
          "AvailableRooms": [
            {
              "RoomCode": "102432",
              "RoomName": "Suite",
              "Occupancy": "4",
              "Price": "3500",
              "Status": "true"
            }
          ]
        }
      ]
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Replace this:

public class AvailableRooms
{
    public object AvailableRoom { get; set; }
}

By:

public class AvailableRooms
{
    public Room[] AvailableRoom { get; set; }
}

public class Room
{
    public string RoomCode { get; set; }
    public string RoomName { get; set; }
    public string Occupancy { get; set; }
    public string Status { get; set; }
}

Note: for some reason, all room fields are strings. I would expect int and bool in some places.

4 Comments

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'Models.Room[]' Path 'avaliabilitiesResponse.Hotels.Hotel[2].AvailableRooms.AvailableRoom.RoomCode', line 77, position 25.' Sorry not working, I am new to JSON deserialization sorry.
According to the JSON you posted, AvailableRoom is an array. In the error I don't see that reflected. Can you show what is in line 77 in the actual JSON?
Line 77 "RoomCode": "102432",
That part is invalid if you see the other AvailableRooms. Those are all arrays.
0

The issue is in your JSON object

"AvailableRooms": {
            "AvailableRoom": [
              {
                "RoomCode": "11245",
                "RoomName": "Standard",
                "Occupancy": "1",
                "Status": "true"
              },
              {
                "RoomCode": "12000",
                "RoomName": "Double",
                "Occupancy": "2",
                "Status": "true"
              },
              {
                "RoomCode": "99685",
                "RoomName": "Twin",
                "Occupancy": "2",
                "Status": "true"
              }
            ]
          }

AvailableRooms is an object here, which has a property called AvailableRoom which is an array. Try removing AvailableRoom property altogether from your json object and keep the array values under AvailableRoom.

If you see your last object in Hotel array

"AvailableRooms": {
            "AvailableRoom": { // This is an array in the first 2 Hotel objects, but an object here.
              "RoomCode": "102432",
              "RoomName": "Suite",
              "Occupancy": "4",
              "Price": "3500",
              "Status": "true"
            }
          }

7 Comments

Why remove the property if you can fix the serialization?
because the properties are all other the place, and as per the naming AvailableRooms is supposed to be an array.
Never mind, I got the problem you pointed to. It just wasn't clear when I read it.
see the person with accepted answer, that's exactly what I meant to say. But people here want to be spoon fed by getting answer to copy/paste rather than understanding the problem.
From my point of view, you should have made clear only the last object was wrong. The others are fine. That is why the code (and code generation) fails.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.