简体   繁体   English

无法将 JSON 数组反序列化为对象

[英]Cannot deserialize the JSON array to object

    string 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<List< Result>>(res);

Cannot deserialize the current JSON object (eg {"name":"value"}) into type 'System.Collections.Generic.List`1[Finnet.Program+ArastirmaRaporListesiResults]' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly.无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List`1[Finnet.Program+ArastirmaRaporListesiResults]',因为该类型需要 JSON 数组(例如 [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 array or List) that can be deserialized from a JSON object.要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或更改反序列化类型,使其成为普通的 .NET 类型(例如,不是像整数这样的原始类型,而不是像这样的集合类型可以从 JSON 对象反序列化的数组或列表。 JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. JsonObjectAttribute 也可以添加到类型中以强制它从 JSON 对象反序列化。 Path 'ArastirmaRaporListesiResult', line 1, position 31.路径“ArastirmaRaporListesiResult”,第 1 行,位置 31。

The first thing i noticed is that your Result class will be trying to bind to a property called "Datas" where as the json string suggests it should be called "Data".我注意到的第一件事是您的 Result 类将尝试绑定到一个名为“Datas”的属性,因为 json 字符串表明它应该被称为“Data”。 Based on the json string I think you should try to deserialize to:基于 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; }
}

For reference I used https://app.quicktype.io/ to generate the class作为参考,我使用https://app.quicktype.io/生成类

public class ArastirmaRaporListesiResult
{
    public List<Datum> Data { get; set; }
    public int ErrorCode { get; set; }
    public object ErrorMessage { get; set; }
    public int StatusCode { get; set; }
}
public 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 int RaporId { get; set; }
    public string RaporTarih { get; set; }
    public string Url { get; set; }
}
public class Root
{
    public ArastirmaRaporListesiResult ArastirmaRaporListesiResult { get; set; }
}

Root myDeserializedClass = JsonConvert.DeserializeObject(myJsonResponse);根 myDeserializedClass = JsonConvert.DeserializeObject(myJsonResponse);

It works in this state.它在这种状态下工作。 Thanks.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM