这是json字符串。
我想从中生成c#类。但是当我通过http://json2csharp.com/尝试时出现问题。
因为room_types有名字而不是类型。我该如何解决这个问题?
{
"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" : "concierge@example.com",
"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."
}
}
}
]
}
生成的类
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; }
}
我发现room_types出了问题。 我该如何创建房型?我在这一点上很困惑。应该是什么类型的“芬威房间”和“联邦房间”?
编辑但是我真正的问题是“如果我有第3个房间类型,我是否需要为它创建课程?”我正在根据给定的json格式开发c#web api。我有很多不同的房型。如:标准房,家庭房,豪华房等。如何根据给定的房型格式准备我的网络API代码?我为每种房型创建一个班级?作为fenway房间,coomonwealt房间。它不应该是房间的普通类吗?
答案 0 :(得分:2)
变量名称不应包含空格。这有效:
{
"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" : "concierge@example.com",
"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."
}
}
}
]
}