【问题标题】:Cannot deserialize the JSON array to object无法将 JSON 数组反序列化为对象
【发布时间】:2022-07-05 17:57:37
【问题描述】:

字符串 res = "{"ArastirmaRaporListesiResult":{"Data":[{"Baslik":"Akbank","DosyaAd":"66245_AKBNK_27062022_OtomatikBUlten.pdf","EnstrumanKod":"AKBNK","KategoriAd":" Şirket Notu","KategoriKod":"SIRKETRAPOR","RaporId":27573,"RaporTarih":"27.06.2022","Url":"http:"},{"Baslik":"Bim Mağazalar","DosyaAd ":"66243_BIMAS_27062022_OtomatikBUlten.pdf","EnstrumanKod":"BIMAS","KategoriAd":"Şirket Notu","KategoriKod":"SIRKETRAPOR","RaporId":27571,"RaporTarih":"27.06.2022"," Url":"http:"}],"ErrorCode":0,"ErrorMessage":null,"StatusCode":200}}";

    public class Result
    {
        public List<Data> Datas { get; set; }
        public int ErrorCode { get; set; }
        public string ErrorMessage { get; set; }
        public int StatusCode { get; set; }
    }

    public class Data
    {
        public string Baslik { get; set; }
        public string DosyaAd { get; set; }
        public string EnstrumanKod { get; set; }
        public string KategoriAd { get; set; }
        public string KategoriKod { get; set; }
        public string RaporId { get; set; }
        public string RaporTarih { get; set; }
        public string Url { get; set; }
    }

var arastirmaContracts = JsonConvert.DeserializeObject>(res);

无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List`1[Finnet.Program+ArastirmaRaporListesiResults]',因为该类型需要 JSON 数组(例如[1,2,3]) 正确反序列化。 要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或更改反序列化类型,使其成为普通的 .NET 类型(例如,不是像整数这样的原始类型,而不是像可以从 JSON 对象反序列化的数组或列表。 JsonObjectAttribute 也可以添加到类型中以强制它从 JSON 对象反序列化。 路径“ArastirmaRaporListesiResult”,第 1 行,位置 31。

【问题讨论】:

  • 错误很明显。这个 JSON 字符串不是一个数组,它是一个具有名为 ArastirmaRaporListesiResult 的属性的对象。使用正确的类进行反序列化或创建一个与 JSON 字符串实际匹配的类
  • 我应该如何更改 Result 和 Data 类。
  • 你没有。您发布的 JSON 字符串包含一个 不同的 对象,一个具有单个 ArastirmaRaporListesiResult 属性的对象,其中包含看起来像 Result 对象的内容。有几个在线 JSON->C# 生成器可以从 JSON 字符串生成 DTO。甚至 Visual Studio 也可以做到这一点,使用编辑菜单中的Paste Special &gt; Paste As JSON 命令

标签: c# json


【解决方案1】:

我注意到的第一件事是您的 Result 类将尝试绑定到一个名为“Datas”的属性,因为 json 字符串表明它应该被称为“Data”。 基于json字符串,我认为您应该尝试反序列化为:

public partial class Result
{
    public ArastirmaRaporListesiResult ArastirmaRaporListesiResult { get; set; }
}

public partial class ArastirmaRaporListesiResult
{
    public Datum[] Data { get; set; }
    public long ErrorCode { get; set; }
    public object ErrorMessage { get; set; }
    public long StatusCode { get; set; }
}

public partial class Datum
{
    public string Baslik { get; set; }
    public string DosyaAd { get; set; }
    public string EnstrumanKod { get; set; }
    public string KategoriAd { get; set; }
    public string KategoriKod { get; set; }
    public long RaporId { get; set; }
    public string RaporTarih { get; set; }
    public string Url { get; set; }
}

作为参考,我使用https://app.quicktype.io/ 生成类

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-08
    • 1970-01-01
    相关资源
    最近更新 更多