0

I need to create clasess from json, but found some strange part that really troubles me. This is example from documentation that i have to response to them. There is some "Fenway Room" that should be variable but it looks it is value (room name) that contain other properties.

{
"api_version" : 4 ,
"hotel_ids" : [97497],
"num_hotels" : 1,
"hotels" :
[
    {
        "hotel_id": 97497,
        "room_types":
        {
        "Fenway Room":
            {
            "url": "",
            "price": 178,
            "fees": 80,
            "fees_at_checkout": 0,
            "taxes": 20,
            "taxes_at_checkout": 0,
            "final_price": 278
            }
        }
    }
]
}

here are classes online generated, and looks wrong. it looks that is not possible.

public class FenwayRoom
{
    public string url { get; set; }
    public int 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 int final_price { get; set; }
}

public class RoomTypes
{
    public FenwayRoom __invalid_name__Fenway Room { get; set; }
}

public class Hotel
{
    public int hotel_id { get; set; }
    public RoomTypes room_types { get; set; }
}

public class RootObject
{
    public int api_version { get; set; }
    public List<int> hotel_ids { get; set; }
    public int num_hotels { get; set; }
    public List<Hotel> hotels { get; set; }
}

This is my first working with json, so im really confused. Json looks valid, but it is possible to create classes from that or i should create response manualy?... joining strings...


added example:
"hotels" :[
    {
    "hotel_id" : 258705 ,
    "room_types" :
        {
            "Room 1" :
            {
                "url" :"", "price" : 1136 , "fees" : 101.55 , "taxes" : 50.00 ,  "final_price" : 1287.55 ,

            }
            "Room 2" :
            {
             "url" :"", "price" : 1136 , "fees" : 101.55 , "taxes" : 50.00 ,  "final_price" : 1287.55 ,
            }
        }
    }
]

Where "Room 1" and "Room 2" are room titles, not properties or variables... it can be anything here.

Davor Mlinaric
  • 1,989
  • 1
  • 19
  • 26

3 Answers3

0

This works with ASP.NET MVC4. I don't know if lower supports it.

public JsonResult MyAPI()
{

    return Json(new RootObject { api_version = 1,
                                 hotel_ids = 12342,
                                 hotels = new Hotel{ hotel_id = 123,
                                                     room_types = new RoomTypes...
                                       }
                                   }
}

As far as Json is an object herited from the class Controller.

Deblaton Jean-Philippe
  • 11,188
  • 3
  • 49
  • 66
0

You can generate classes from json by creating a new class, copying the json out, go to Edit->paste special->paste json as classes

Then you will get this: class Class1 {

    public class Rootobject
    {
        public int api_version { get; set; }
        public int[] hotel_ids { get; set; }
        public int num_hotels { get; set; }
        public Hotel[] hotels { get; set; }
    }

    public class Hotel
    {
        public int hotel_id { get; set; }
        public Room_Types room_types { get; set; }
    }

    public class Room_Types
    {
        public FenwayRoom FenwayRoom { get; set; }
    }

    public class FenwayRoom
    {
        public string url { get; set; }
        public int 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 int final_price { get; set; }
    }

}
TheWall
  • 1
  • 1
  • "FenwayRoom" is not property, it's text "Fenway Room" it can be any text here.... please check original json i posted. – Davor Mlinaric Aug 26 '13 at 11:13
  • The class I posted is autogenerated by visual studio from the json you posted. If the class is incorrect, then your json file must be incorrect. – TheWall Aug 26 '13 at 12:06
  • yes, it's valid but looks incorrect. but i need to return like that, because it's on documentation of some other company. i can't change that. so i'm looking for solution... last option is to manualy generate that json by joining strings. – Davor Mlinaric Aug 26 '13 at 12:11
0

try this one out http://json2csharp.com/

http://json2csharp.com/  

   Paste  your Json String there it will generate the Json Class
Aravind
  • 1,521
  • 2
  • 12
  • 23