Bilal
Bilal

Reputation: 141

How to fix json conversion to c# class?

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.

Upvotes: 1

Views: 75

Answers (3)

Risto M
Risto M

Reputation: 3009

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

Upvotes: 2

CoOl
CoOl

Reputation: 2797

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

Upvotes: 0

Patrick Hofman
Patrick Hofman

Reputation: 157098

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.

Upvotes: 1

Related Questions