0

I want to iterate this JSON and convert it to C# object but problem is standard_room and executive_room are dynamic, they can be anything, other room types also, so I cannot create any classes at design time.

{  
"city":"Bengaluru",
"country":"India",
"start_date":"2015-05-23",
"end_date":"2015-05-25",
"party":[  
   {  }
],
"num_hotels":1,
"hotels":[  
  {  
     "hotel_id":"2210648",
     "name":"Fortune Select JP Cosmos",
     "street":"No 49 Cunningham Crescent Road Behind Sigma Mall",
     "city":"Bengaluru",
     "postal_code":"560044",
     "state":"Karnataka",
     "country":"India",
     "latitude":12.98971,
     "longitude":77.59463,
     "phone":"91 (20) 65008171",
     "room_types":{  
        "Standard_Room":{  
           "price":13136.200000,
           "fees":0,
           "fees_at_checkout":0,
           "taxes":0,
           "taxes_at_checkout":0,
           "final_price":13136.200000,
           "currency":"INR",
           "num_rooms":1
        },
        "Executive_Room":{  
           "price":10771.684000,
           "fees":0,
           "fees_at_checkout":0,
           "taxes":0,
           "taxes_at_checkout":0,
           "final_price":10771.684000,
           "currency":"INR",
           "num_rooms":1
        }
     }
  }
]
 }
public class Party
{
    public int adults { get; set; }
}

public class RoomDetails
{
    public double price { get; set; }
    public int fees { get; set; }
    public int fees_at_checkout { get; set; }
    public int taxes { get; set; }
    public int taxes_at_checkout { get; set; }
    public double final_price { get; set; }
    public string currency { get; set; }
    public int num_rooms { get; set; }
}

public class Hotel
{
    public string hotel_id { get; set; }
    public string name { get; set; }
    public string street { get; set; }
    public string city { get; set; }
    public string postal_code { get; set; }
    public string state { get; set; }
    public string country { get; set; }
    public double latitude { get; set; }
    public double longitude { get; set; }
    public string phone { get; set; }
    public RoomTypes room_types { get; set; }
}

public class RoomTypes
{
    public RoomDetails Standard_Room { get; set; }
    public RoomDetails Executive_Room { get; set; }
}

public class HotelsAvaibility
{
    public string city { get; set; }
    public string country { get; set; }
    public string start_date { get; set; }
    public string end_date { get; set; }
    public List<Party> party { get; set; }
    public string query_key { get; set; }
    public int num_hotels { get; set; }
    public List<Hotel> hotels { get; set; }
}

I have assigned strJson with above JSON.

HotelsAvaibility obj = JsonConvert.DeserializeObject<HotelsAvaibility>(str JSON);
Biffen
  • 6,249
  • 6
  • 28
  • 36

0 Answers0