c# - 如何修复 json 到 c# 类的转换?

标签 c# json

我正在尝试从 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 的特殊粘贴选项进行类转换,但我的对象中仍然没有可用空间。

最佳答案

这是固定的 json(问题是最后一个 AvailableRooms,它有一个无效的成员 AvailableRoom,它既是数组又是对象)。可以使用 visual studio 将此 JSON 粘贴为 json-as-classes:

{
  "avaliabilitiesResponse": {
    "Hotels": {
      "Hotel": [
        {
          "HotelCode": "0017855",
          "HotelsName": "Hilton Jeddah",
          "Location": "Jeddah, Saudi Arabia",
          "Rating": "4",
          "LowestPrice": "35",
          "Currency": "EUR",
          "IsReady": "true",
          "AvailableRooms": 
            [
              {
                "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" :  
            [
              {
                "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": [
            {
              "RoomCode": "102432",
              "RoomName": "Suite",
              "Occupancy": "4",
              "Price": "3500",
              "Status": "true"
            }
          ]
        }
      ]
    }
  }
}

关于c# - 如何修复 json 到 c# 类的转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55536277/

相关文章:

c# - "A lambda expression with a statement body cannot be converted to an expression tree"

c# - Kendo UI TreeListDataSource Read() 仅在本地运行时有效

c# - 在 MSTest 中处理单元测试中的预期异常

java - 使用gson在java中写入json文件

java - Angular JS $http.post 方法导致 JSON 数组格式错误

c# - 如何在 SonarQube 中排除项目特定文件

c# - 如何像在 C++ 中一样在 C# 中创建引用成员变量

javascript - 使用变量创建 json 对象

ruby - 使用 jbuilder 创建具有动态哈希键的 JSON

json - 为什么仅为 POST 请求/201(已创建)响应设置 HTTP 位置 header ?