2

Here is the json string.

I would like to generate c# classes from it. But there is a problem when I tried via http://json2csharp.com/.

Because room_types has names but not type. How can I solve this issue?

{
"api_version" : 5,
"lang" : "en_US",
"hotels" :
    [
        {
            "ta_id" : 258705 ,
            "partner_id" : "hc",
            "name" : "Hotel Commonwealth",
            "street" : "500 Commonwealth Avenue",
            "city" : "Boston",
            "postal_code" : "02215",
            "state" : "Massachusetts",
            "country" : "USA",
            "latitude" : 42.348808,
            "longitude" : -71.094971,
            "desc" : "The Hotel Commonwealth stands above the Kenmore Square \"T\" subway station in Boston, Mass. Fenway Park is located two blocks away, while the shops along Newbury Street are three blocks from the hotel.",
            "amenities" : ["RESTAURANT","NON_SMOKING"],
            "url" : "http://www.partner-site.com/hotelcommonwealth",
            "email" : "[email protected]",
            "phone" : "555-555-5555",
            "fax" : "555-555-5555",
            "room_types"  :
                {
                    "Fenway Room" :
                        {
                            "url" : "http://www.partner-site.com/hotel_commonwealth/fenway_room",
                            "desc" : "One king bed with pillowtop mattress, Frette Italian linens, down bedding, multiple pillows. View of Fenway Park."
                        },
                    "Commonwealth Room" :
                        {
                            "url" : "http://www.partner-site.com/hotel_commonwealth/commonwealth_room",
                            "desc" : "One king bed with pillowtop mattress, Frette Italian linens, down bedding, multiple pillows. View of Commonwealth Avenue."
                        }
                }
        }
    ]
}

And here is the classes which I generated via http://json2csharp.com/

 public class FenwayRoom
 {
public string url { get; set; }
public string desc { get; set; }
  }

 public class CommonwealthRoom
  {
public string url { get; set; }
public string desc { get; set; }
 }

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

public class Hotel
  {
public int ta_id { get; set; }
public string partner_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 desc { get; set; }
public List<string> amenities { get; set; }
public string url { get; set; }
public string email { get; set; }
public string phone { get; set; }
public string fax { get; set; }
public RoomTypes room_types { get; set; }
 }

 public class RootObject
 {
public int api_version { get; set; }
public string lang { get; set; }
public List<Hotel> hotels { get; set; }
 }

I see that something is wrong in room_types. How should I create room types class? I am confused at this point. what should be the type of "Fenway room" and "commonwealth room" ?

EDIT But but my real problem is that "if I have a 3rd room type, Do I need to create a class for it?" I am developing c# web api according to given json format. I have many different room types. such as : standart room, family room, deluxe room etc. how can I prepare my web api code according to given room type format? shal I create a class for every room type? as fenway room, coomonwealt room. Shouldn't it be a genereic class for rooms?

4
  • Try to replace spaces with underscores in the JSON names. Commented Oct 28, 2015 at 15:58
  • Just replacing the spaces, as suggested in the answer, would solve it. But you say "Because room_types has names but not type" which sounds like a structural problem rather than just generating classes Commented Oct 28, 2015 at 16:21
  • On a side note. Please review your naming conventions. msdn.microsoft.com/en-us/library/vstudio/… Commented Oct 28, 2015 at 16:24
  • Thanks for the feedbacks. But but my real problem is that "if I have a 3rd room type, Do I need to create a class for it?" I am developing c# web api according to given json format. I have many different room types. such as : standart room, family room, deluxe room etc. how can I prepare my web api code according to given room type format? shal I create a class for every room type? as fenway room, coomonwealt room. Shouldn't it be a genereic class for rooms? Commented Oct 31, 2015 at 9:23

1 Answer 1

2

Variable names should not have spaces. This works:

{
"api_version" : 5,
"lang" : "en_US",
"hotels" :
    [
        {
            "ta_id" : 258705 ,
            "partner_id" : "hc",
            "name" : "Hotel Commonwealth",
            "street" : "500 Commonwealth Avenue",
            "city" : "Boston",
            "postal_code" : "02215",
            "state" : "Massachusetts",
            "country" : "USA",
            "latitude" : 42.348808,
            "longitude" : -71.094971,
            "desc" : "The Hotel Commonwealth stands above the Kenmore Square \"T\" subway station in Boston, Mass. Fenway Park is located two blocks away, while the shops along Newbury Street are three blocks from the hotel.",
            "amenities" : ["RESTAURANT","NON_SMOKING"],
            "url" : "http://www.partner-site.com/hotelcommonwealth",
            "email" : "[email protected]",
            "phone" : "555-555-5555",
            "fax" : "555-555-5555",
            "room_types"  :
                {
                    "FenwayRoom" :
                        {
                            "url" : "http://www.partner-site.com/hotel_commonwealth/fenway_room",
                            "desc" : "One king bed with pillowtop mattress, Frette Italian linens, down bedding, multiple pillows. View of Fenway Park."
                        },
                    "CommonwealthRoom" :
                        {
                            "url" : "http://www.partner-site.com/hotel_commonwealth/commonwealth_room",
                            "desc" : "One king bed with pillowtop mattress, Frette Italian linens, down bedding, multiple pillows. View of Commonwealth Avenue."
                        }
                }
        }
    ]
}
Sign up to request clarification or add additional context in comments.

Comments

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.