获取List中具有多种数据类型的JSON API扩展

时间:2018-05-29 06:54:24

标签: c# json api uwp

我现在正在一个uwp项目中工作,我从JSON Web API获取值。在API里面我有多个房间和属性,我没有遇到它们的问题。

我遇到的问题是如何获取房间内所有预订的价值,因为预订是在这样的网址扩展名中:

没有预订的网址:

string url = "https://api.booking.com/api/room";

预订网址:

string url = "https://api.booking.com/api/room/35bf3c4d-9b5b-40fd-bcf4-a4c2c6c564bc/bookings";

Guid是房间的ID。

我对每个房间的图像都有同样的问题,但我通过这样解决了这个问题:

这是我首先测试但后来只有一张图片:

string url = "https://api.booking.com/api/room/35bf3c4d-9b5b-40fd-bcf4-a4c2c6c564bc/image";

但我测试了这个,它得到了所有的图像:

public Uri ImageUrl => 
   new Uri( $"https://api.booking.com/api/room/{Id}/image", UriKind.Absolute );

所以我得到了我所拥有的每个房间的ID,并将图像提供给所有房间,但这种方法不适用于预订因为在预订扩展中有多种数据类型,如果我不能工作使对象成为uri

希望你理解我的问题......

提前致谢!

编辑:

两个房间的Json样本:

[
{
    "name": "Rum 1",
    "id": "a31d1fc8-df29-419c-8308-f8bc884b378e",
    "seats": 10,
    "availableFrom": null,
    "availableTo": null
  },
  {
    "name": "Rum 2",
    "id": "7defd34d-222d-4980-b28f-e616e8b9003c",
    "seats": 5,
    "availableFrom": null,
    "availableTo": null
  }
]

其中一个预订的Json样本:

[
  {
    "id": "12324a01-92b7-49f3-8d10-1def59eddd32",
    "code": 6679,
    "timeFrom": "2018-02-13T16:00:00",
    "timeTo": "2018-02-13T16:45:00",
    "note": null,
    "createdDate": "2018-02-13T10:04:14.8",
    "room": {
      "name": "Rum 2",
      "id": "7defd34d-222d-4980-b28f-e616e8b9003c",
      "seats": 10,
      "availableFrom": null,
      "availableTo": null
    }
  }
]

这是我的预订课程:

public class Bookings
{
    public string id { get; set; }
    public int code { get; set; }
    public DateTime timeFrom { get; set; }
    public DateTime timeTo { get; set; }
    public List<Class2> room { get; set; }
}

这是房间的课程:

public class Class2
{
    public string name { get; set; }
    public string id { get; set; }
    public int seats { get; set; }
    public Uri ImageUrl => new Uri($"https://api.bookings.com/api/rooms/{id}/image", UriKind.Absolute);
}

这就是我预订的方式:

string booking = "https://api.booking.com/api/room/{id}/bookings";

HttpClient BookingClient = new HttpClient();

string BookingResponse = await BookingClient.GetStringAsync(booking);

var BookingData = JsonConvert.DeserializeObject<List<Bookings>>(response);

这是我宣布变量室和书籍的地方:

foreach (var room in data)
{
    string id = room.id;
    string name = room.name;
    int seats = room.seats;
    Uri Img = room.ImageUrl;

    foreach (var books in BookingData)
    {
        string note = books.note;
        DateTime TimeFrom = books.timeFrom;
        DateTime TimeTo = books.timeTo;
        Class2 BookRoom = books.room;
        string BookId = books.id;
        int code = books.code;

        //if (room.Bookings.Any(a => a.timeFrom == DateTime.UtcNow))
        //{
        //    templategrid.GreenToRed();
        //}
        //else if (room.Bookings.Any(a => a.timeFrom != DateTime.UtcNow))
        //{
        //    do stuff
        //}
    }
}

1 个答案:

答案 0 :(得分:1)

问题可能在Bookings类:

public class Bookings
{
    public string id { get; set; }
    public int code { get; set; }
    public DateTime timeFrom { get; set; }
    public DateTime timeTo { get; set; }
    public Class2 room { get; set; }
}

根据您发布的示例JSON,room属性不是List或数组 - 它只是一个单独的房间。我还建议您将类Class2的名称更改为BookingRoom之类的名称,以使代码更具可读性。

要从bookings端点读取数据,您可以执行

var bookings = JsonConvert.DeserializeObject<Bookings[]>( jsonString );

更新:我刚刚测试了以与您相同的方式对类进行反序列化,并且对日期进行了反序列化:

Deserializing

我使用了完全相同的类:

public class Bookings
{
    public string id { get; set; }
    public int code { get; set; }
    public DateTime timeFrom { get; set; }
    public DateTime timeTo { get; set; }
    public Class2 room { get; set; }
}

public class Class2
{
    public string name { get; set; }
    public string id { get; set; }
    public int seats { get; set; }
    public Uri ImageUrl => new Uri($"https://api.rumsbokning.nu/api/companies/aab96aa1-d8ca-4f74-8e35-ded190c38dd4/rooms/{id}/image", UriKind.Absolute);
}

<强>更新: 您必须循环获取房间预订并手动关联它们:

foreach (var room in data)
{
   string booking = $"https://api.booking.com/api/room/{room.Id}/bookings";
   HttpClient BookingClient = new HttpClient();
   string BookingResponse = await BookingClient.GetStringAsync(booking);
   var bookings = JsonConvert.DeserializeObject<List<Bookings>>(response);
   room.Bookings = bookings;
}

您的代码当前正在执行的是枚举每个房间,并且每个房间都会枚举相同的BookingsData变量,但这不起作用,因为该变量只包含其中一个房间,不是所有人。