簡體   English   中英

我無法反序列化 JSON

[英]I can't deserialize the JSON

我正在嘗試連接到 PostNL API 時間幀。 問題是我不斷收到以下異常。 我想我的模型是正確的,但我似乎無法找出它有什么問題。

例外:

Cannot deserialize the current JSON object (eg {"name":"value"}) into type 'System.Collections.Generic.List`1[PostNL.Api.Dtos.TimeFrameHolderDto]' because the type requires a JSON array (eg [ 1,2,3]) 正確反序列化。 To fix this error either change the JSON to a JSON array (eg [1,2,3]) or change the deserialized type so that it is a normal .NET type (eg not a primitive type like integer, not a collection type like an數組或列表)可以從 JSON object 反序列化。 JsonObjectAttribute 也可以添加到類型中以強制它從 JSON object 反序列化。 Path 'Timeframes.Timeframe', line 3, position 16. at PostNL.Api.Tests.Unit.Tests.Unit.TimeFramesTests.TimeFrameResponseJsonTests.JsonConvertTests(String filePath) in C:\Git\PostNL.Api.Tests.Unit\TimeFramesTests \TimeFrameResponseJsonTests.cs:第 37 行

如果我讀到錯誤,我需要創建更多帶有持有者和列表的模型,如下所示,但我仍然遇到這個異常。 有誰知道我做錯了什么?

請求/1.json 文件:

{
  "Timeframes": {
    "Timeframe": [
      {
        "Date": "22-08-2022",
        "Timeframes": {
          "TimeframeTimeFrame": [
            {
              "From": "08:45:00",
              "Options": {
                "string": "Daytime"
              },
              "To": "11:15:00"
            },
            {
              "From": "17:30:00",
              "Options": {
                "string": "Evening"
              },
              "To": "22:00:00"
            }
          ]
        }
      },
      {
        "Date": "23-08-2022",
        "Timeframes": {
          "TimeframeTimeFrame": [
            {
              "From": "09:15:00",
              "Options": {
                "string": "Daytime"
              },
              "To": "11:45:00"
            },
            {
              "From": "17:30:00",
              "Options": {
                "string": "Evening"
              },
              "To": "22:00:00"
            }
          ]
        }
      }
    ]
  }
}

反序列化測試:

using PostNL.Api.Dtos;
using Newtonsoft.Json;
using NUnit.Framework;
using System;
using System.IO;

namespace PostNL.Api.Tests.Unit.TimeFramesTests
{
    [TestFixture]
    [Parallelizable(ParallelScope.All)]
    internal class TimeFrameResponseJsonTests
    {
        [Test]
        [TestCase("../../../TimeFramesTests/requests/1.json")]
        public void JsonConvertTests(string filePath)
        {
            var path = AppDomain.CurrentDomain.BaseDirectory;
            var fullFilePath = Path.Combine(path, filePath);
            if (!File.Exists(fullFilePath))
            {
                Assert.Fail("File does not exists");
                return;
            }

            try
            {
                var fileContent = File.ReadAllText(filePath);
                var json = JsonConvert.DeserializeObject<TimeFrameResponseDto>(fileContent);
            }
            catch (Exception exception)
            {
                Assert.Fail(exception.Message);
            }

            Assert.Pass("Successful convert");
        }
    }
}

型號:

using Newtonsoft.Json;
using System.Collections.Generic;

namespace PostNL.Api.Dtos
{
    internal class TimeFrameDto
    {
        [JsonProperty("From")]
        public string From { get; set; }
        [JsonProperty("Options")]
        public OptionDto[] Options { get; set; }
        [JsonProperty("To")]
        public string To { get; set; }
    }

    internal class TimeFrameTimeFrameHolder
    {
        [JsonProperty("TimeframeTimeFrame")]
        public List<TimeFrameDto> TimeFrameTimeFrame { get; set; }
    }

    internal class DayTimeFrameDto
    {
        [JsonProperty("Date")]
        public string Date { get; set; }
        [JsonProperty("Timeframes")]
        public List<TimeFrameTimeFrameHolder> TimeFrameDtos { get; set; }
    }

    internal class TimeFrameHolderDto
    {
        [JsonProperty("Timeframe")]
        public List<DayTimeFrameDto> TimeFrames { get; set; }
    }

    internal class TimeFrameResponseDto : PostNLBaseDto
    {
        [JsonProperty("Timeframes")]
        public List<TimeFrameHolderDto> TimeFrames { get; set; }

        //[JsonProperty("ReasonNoTimeframes")]
        //public List<ReasonNoTimeFrameDto> ReasonNoTimeFrames { get; set; }
    }
}

你還需要一個 class - DataTimeframesDto

TimeFramesResponseDTo timeFramesResponseDTo=JsonConvert.DeserializeObject<TimeFramesResponseDTo>(fileContent);

public partial class TimeFramesResponseDTo
{
    [JsonProperty("Timeframes")]
    public DataTimeframesDto Timeframes { get; set; }
}

public partial class DataTimeframesDto
{
    [JsonProperty("Timeframe")]
    public List<DayTimeFrameDto> Timeframe { get; set; }
}

更新

我的課的 rest 你還是不能用你的


public partial class DayTimeFrameDto
{
    [JsonProperty("Date")]
    public string Date { get; set; }

    [JsonProperty("Timeframes")]
    public TimeFrameTimeFrameHolder Timeframes { get; set; }
}

public partial class TimeFrameTimeFrameHolder
{
    [JsonProperty("TimeframeTimeFrame")]
    public List<TimeframeDTo> TimeFrameTimeFrame{ get; set; }
}

public partial class TimeframeDTo
{
    [JsonProperty("From")]
    public DateTime From { get; set; }

    [JsonProperty("Options")]
    public OptionsDto Options { get; set; }

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

public partial class OptionsDto
{
    [JsonProperty("string")]
    public string OptionsString { get; set; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM