我正在尝试从json生成类以反序列化为c#对象,但列表对象不正确,我在哪里出错?根据我的假设,可用房间应为清单,但不起作用。
下面是我的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"
}
}
}
]
}
}
}
它像这样转换为c#。
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; }
}
我尝试使用Json2csharp以及Visual Studio的粘贴特殊选项进行类转换,但是我仍然无法在对象中获得可用的房间。
答案 0 :(得分:2)
这是固定的json(问题是最后一个### Create order [POST]
Create a new order
+ Request Post new patient (application/json)
+ Attributes (object)
### CustomIds
+ system: `['Some1', 'Some2']` (string, optional) - The order custom id's system
+ Members
+ `Test1`
+ `Test2`
+ `Test3`
+ customerName: `John` (string, required) - The customer's given name
+ customIds: `some, 123` (array[CustomIds], optional) - custom ids
+ Body
{
"customerName" : "John",
"customIds": [{"system": "some", "id": "123"}] //optional
}
,其成员AvailableRooms
既是数组又是对象)无效。可以使用Visual Studio将此JSON粘贴为类:
AvailableRoom
答案 1 :(得分:1)
替换此:
public class AvailableRooms
{
public object AvailableRoom { get; set; }
}
通过:
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; }
}
注意:由于某些原因,所有房间字段都是字符串。我希望在某些地方使用int
和bool
。
答案 2 :(得分:0)
问题出在您的JSON对象中
"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
是此处的对象,它具有称为AvailableRoom
的属性,该属性是一个数组。尝试从您的json对象中完全删除AvailableRoom
属性,并将数组值保留在AvailableRoom
下。
如果您看到Hotel
数组中的最后一个对象
"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"
}
}