API 调用 - 难以从反序列化 JSON 引用公共列表

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

我试图了解如何使用 API 调用中的数据。我可以引用“Booking”中的所有内容,除了“Item”的内容 - 特别是“startTime”。

var APIContent = response.Content.ReadAsStringAsync().Result;
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(APIContent);

public class Booking
{
    public string bookingReference { get; set; }
    public string uniqueId { get; set; }
    public DateTime createdDate { get; set; }
    public string status { get; set; }
    public string name { get; set; }
    public int customerId { get; set; }
    public double total { get; set; }
    public List<Item> items { get; set; }
    public List<object> customerFlags { get; set; }
}

public class Item
{
    public int productId { get; set; }
    public int quantity { get; set; }
    public string bookingDate { get; set; }
    public string startTime { get; set; }
}

public class Root
{
    public List<Booking> bookings { get; set; }
}

如上所述,我需要从“startTime”中提取字符串,但我不确定如何引用它。当单步执行代码时,我可以看到数据就在那里。有人能指出我正确的方向吗?

编辑:

    {
        "bookings": [
            {
                "bookingReference": "13092675",
                "uniqueId": "1537d669-1a11-487a-b132-b9198a822956",
                "createdDate": "2024-03-10T18:26:16.943Z",
                "status": "Paid",
                "name": "Test Data",
                "customerId": 03737676,
                "total": 321.35,
                "items": [
                    {
                        "productId": 188460,
                        "quantity": 20,
                        "bookingDate": "2024-03-15",
                        "startTime": "11:00"
                    },
                    {
                        "productId": 180344,
                        "quantity": 20,
                        "bookingDate": "2024-03-15"
                    },
                    {
                        "productId": 180343,
                        "quantity": 20,
                        "bookingDate": "2024-03-15"
                    },
                    {
                        "productId": 180310,
                        "quantity": 1,
                        "bookingDate": "2024-03-15"
                    },
                    {
                        "productId": 111193,
                        "quantity": 1,
                        "bookingDate": "2024-03-15"
                    },
                    {
                        "productId": 194193,
                        "quantity": 1,
                        "bookingDate": "2024-03-15"
                    },
                    {
                        "productId": 194194,
                        "quantity": 1,
                        "bookingDate": "2024-03-15"
                    },
                    {
                        "productId": 194194,
                        "quantity": 1,
                        "bookingDate": "2024-03-15"
                    },
                    {
                        "productId": 194693,
                        "quantity": 1,
                        "bookingDate": "2024-03-15"
                    },
                    {
                        "productId": 194694,
                        "quantity": 1,
                        "bookingDate": "2024-03-15"
                    },
                    {
                        "productId": 194710,
                        "quantity": 1,
                        "bookingDate": "2024-03-15"
                    }
                ],
                "customerFlags": []
            },
    ```
c# json list api
1个回答
0
投票

您可以从

first
预订中的first项目参考startTime,如下所示:

var startTime = myDeserializedClass.bookings[0].items[0].startTime;

由于

bookings
items
List
,因此您可以通过索引访问它们。 例如。要迭代所有预订的所有项目的开始时间,您可以执行以下操作:

for (var bookingIndex = 0; bookingIndex < myDeserializedClass.bookings.Count; ++bookingIndex)
{
    var booking = myDeserializedClass.bookings[bookingIndex];
    for (var itemIndex = 0; itemIndex < booking.items.Count; ++itemIndex)
    {
        var startTime = booking.items[itemIndex].startTime;
    }
}

如果您想将每个预订中每个项目的开始时间提取到集合中,您可以使用 Linq

var startTimes = myDeserializedClass.bookings.SelectMany(booking => booking.items.Select(item => item.startTime)).ToList();
© www.soinside.com 2019 - 2024. All rights reserved.