如何使用带有 Newtonsoft.Json 的对象从 API 获取所有数据,我在 xamarin c# 中收到错误数据

How to get all data from API using objects with Newtonsoft.Json and I receive wrong data in xamarin c#

我尝试在 xamarin 中从 API 获取数据,但我只收到数组中的第一个数据。

这是 API 这是代码的屏幕截图:

为什么在 waterData 中我只收到第一个值以及日期和 AlertLevelForecast 不正确?

我的对象看起来像:

using System;
using Newtonsoft.Json;

namespace MaritsaTundzhaForecast
{
    public class WaterDataJson
    {
        [JsonProperty("ardaforecast")]
        public List[] ArdaForecast { get; set; }

        
        [JsonProperty("Dt")]
        public DateTime DateTimeForecast { get; set; }

        [JsonProperty("AL")]
        public int AlertLevelForecast { get; set; }
        
    }

    
    public class List
    {
        [JsonProperty("Dt")]
        public string DateTimeForecast { get; set; }

        [JsonProperty("AL")]
        public int AlertLevelForecast { get; set; }
    }
    

}

我的 RestService class 看起来像:

using System;
using System.Diagnostics;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace MaritsaTundzhaForecast
{
    public class RestService
    {
        HttpClient _client;

        public RestService()
        {
            _client = new HttpClient();
        }

        public async Task<WaterDataJson> GetWaterData(string query)
        {
            WaterDataJson waterData = new WaterDataJson();

            try
            {
                var response = await _client.GetAsync(query);
                if (response.IsSuccessStatusCode)
                {
                    var content = await response.Content.ReadAsStringAsync();
                    waterData = JsonConvert.DeserializeObject<WaterDataJson>(content);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("\t\tERROR {0}", ex.Message);
            }

            return waterData;
        }
    }
}

为什么我只收到一个错误的AlertLevelForecast和一个错误的DateTimeForecast

我应该在对象 class 中做什么才能获得具有正确日期和值的所有数据?

我尝试对其进行反序列化,发现使用 newtonsoft json 进行反序列化的唯一方法是创建此 类(此解决方案已使用 Visual Studio 和 Postman 进行测试)

    public class WaterDataJson
    {
        public List<ForecastBody> Ardaforecast { get; set; }
    }
    public class ForecastBody
    {
        public ForecastItem[] Items { get; set; }
        public ForecastDetails Details { get; set; }
    }

   public class ForecastDetails
    {
        public int fieldCount { get; set; }
        public int affectedRows { get; set; }
        public int insertId { get; set; }
        public int serverStatus { get; set; }
        public int warningCount { get; set; }
        public int changedRows { get; set; }
        public string message { get; set; }
        public bool protocol41 { get; set; }
    }
    public class ForecastItem
    {
        [JsonProperty("Dt")]
        public DateTime DateTimeForecast { get; set; }

        [JsonProperty("AL")]
        public int AlertLevelForecast { get; set; }
    }

json会是这样

{"ardaforecast":[ { "items": [{"Dt":"2021-07-22T23:00:00.000Z","AL":1},{"Dt":"2021-07-23T02:00:00.000Z","AL":1},{"Dt":"2021-07-23T05:00:00.000Z","AL":1},{"Dt":"2021-07-23T08:00:00.000Z","AL":1},{"Dt":"2021-07-23T11:00:00.000Z","AL":1},{"Dt":"2021-07-23T14:00:00.000Z","AL":1},{"Dt":"2021-07-23T17:00:00.000Z","AL":1},{"Dt":"2021-07-23T20:00:00.000Z","AL":1},{"Dt":"2021-07-23T23:00:00.000Z","AL":1},{"Dt":"2021-07-24T02:00:00.000Z","AL":1},{"Dt":"2021-07-24T05:00:00.000Z","AL":1},{"Dt":"2021-07-24T08:00:00.000Z","AL":1},{"Dt":"2021-07-24T11:00:00.000Z","AL":1},{"Dt":"2021-07-24T14:00:00.000Z","AL":1},{"Dt":"2021-07-24T17:00:00.000Z","AL":1},{"Dt":"2021-07-24T20:00:00.000Z","AL":1},{"Dt":"2021-07-24T23:00:00.000Z","AL":1},{"Dt":"2021-07-25T02:00:00.000Z","AL":1},{"Dt":"2021-07-25T05:00:00.000Z","AL":1},{"Dt":"2021-07-25T08:00:00.000Z","AL":1},{"Dt":"2021-07-25T11:00:00.000Z","AL":1},{"Dt":"2021-07-25T14:00:00.000Z","AL":1},{"Dt":"2021-07-25T17:00:00.000Z","AL":1},{"Dt":"2021-07-25T20:00:00.000Z","AL":1},{"Dt":"2021-07-25T23:00:00.000Z","AL":1},{"Dt":"2021-07-26T02:00:00.000Z","AL":1},{"Dt":"2021-07-26T05:00:00.000Z","AL":1},{"Dt":"2021-07-26T08:00:00.000Z","AL":1},{"Dt":"2021-07-26T11:00:00.000Z","AL":1},{"Dt":"2021-07-26T14:00:00.000Z","AL":1},{"Dt":"2021-07-26T17:00:00.000Z","AL":1},{"Dt":"2021-07-26T20:00:00.000Z","AL":1},{"Dt":"2021-07-26T23:00:00.000Z","AL":1},{"Dt":"2021-07-27T02:00:00.000Z","AL":1},{"Dt":"2021-07-27T05:00:00.000Z","AL":1}],"details":{"fieldCount":0,"affectedRows":0,"insertId":0,"serverStatus":34,"warningCount":0,"message":"","protocol41":true,"changedRows":0}}]}

如果您有控制权,最简单的方法是更改​​ API,如果没有,您可以使用 String.Replace

更改 json 输出
 var content = await response.Content.ReadAsStringAsync();
                   
var json = content.Replace("\"ardaforecast\":[[", "\"ardaforecast\":[ {\"items\": [")
                               .Replace("}],{\"fieldCount\"", "}],\"details\":{\"fieldCount\"")
                               .Replace("}]}", "}}]}");
 waterData = JsonConvert.DeserializeObject<WaterDataJson>(json);