如何修复json转换为c#类?

问题描述 投票:1回答:3

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

c# json
3个回答
2
投票

这里是固定的json(问题是最后一个AvailableRooms,它有一个无效的成员AvailableRoom,它既是数组又是对象)。这个JSON可以用visual studio粘贴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"
            }
          ]
        }
      ]
    }
  }
}

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; }
}

注意:由于某种原因,所有房间字段都是字符串。在某些地方我会期待intbool


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"
            }
          }
© www.soinside.com 2019 - 2024. All rights reserved.