简体   繁体   English

如何解决“无法将当前的 JSON 数组反序列化为类型,因为该类型需要 JSON object 才能正确反序列化。”

[英]How to solve “Cannot deserialize the current JSON array into type because the type requires a JSON object to deserialize correctly.”

I'm pretty new into the Xamarin and Web Services world, I´ve been trying to solve a deserialization issue trying but keep getting the error above, please find down below the JSON response, model and view model code. I'm pretty new into the Xamarin and Web Services world, I´ve been trying to solve a deserialization issue trying but keep getting the error above, please find down below the JSON response, model and view model code. I've been trying several solutions but nothing helps.我一直在尝试几种解决方案,但没有任何帮助。 I think it migth be something on my model but I'm not completely sure.我认为这可能是我的 model 上的东西,但我不完全确定。 Any help would be really appreciated.任何帮助将非常感激。

JSON Response JSON 响应

{
    "$id": "1",
    "$values": [
        {
            "$id": "2",
            "Contrato_Id": "FRANOPRA",
            "Emision_Id": "BANME  0159755",
            "Tipo_Custodia_Id": 100,
            "Institucion_Deposito_Id": 5,
            "Portafolio_Tit_Imp": 94768.0,
            "Portafolio_Costo_Promedio": 17.895753297132128,
            "Portafolio_Comprometido": 0.0,
            "Portafolio_TIR": 0.0,
            "Portafolio_Plazo": 0.0,
            "Contrato": null
        },
        {
            "$id": "3",
            "Contrato_Id": "FRANOPRA",
            "Emision_Id": "DOIXCB 15U",
            "Tipo_Custodia_Id": 100,
            "Institucion_Deposito_Id": 5,
            "Portafolio_Tit_Imp": 13000.0,
            "Portafolio_Costo_Promedio": 13.0,
            "Portafolio_Comprometido": 0.0,
            "Portafolio_TIR": 0.0,
            "Portafolio_Plazo": 0.0,
            "Contrato": null
        },
        {
            "$id": "4",
            "Contrato_Id": "FRANOPRA",
            "Emision_Id": "EFECTIVO",
            "Tipo_Custodia_Id": 5,
            "Institucion_Deposito_Id": 5,
            "Portafolio_Tit_Imp": 1.1271450007334352,
            "Portafolio_Costo_Promedio": 1.0,
            "Portafolio_Comprometido": 0.0,
            "Portafolio_TIR": 0.0,
            "Portafolio_Plazo": 0.0,
            "Contrato": null
        },
        {
            "$id": "5",
            "Contrato_Id": "FRANOPRA",
            "Emision_Id": "FT-LIQUA",
            "Tipo_Custodia_Id": 100,
            "Institucion_Deposito_Id": 5,
            "Portafolio_Tit_Imp": 692631.0,
            "Portafolio_Costo_Promedio": 1.007334,
            "Portafolio_Comprometido": 692631.0,
            "Portafolio_TIR": 0.0,
            "Portafolio_Plazo": 0.0,
            "Contrato": null
        },
        {
            "$id": "6",
            "Contrato_Id": "FRANOPRA",
            "Emision_Id": "TEMAGIALX",
            "Tipo_Custodia_Id": 100,
            "Institucion_Deposito_Id": 5,
            "Portafolio_Tit_Imp": 456250.0,
            "Portafolio_Costo_Promedio": 456.72739371178523,
            "Portafolio_Comprometido": 0.0,
            "Portafolio_TIR": 0.0,
            "Portafolio_Plazo": 0.0,
            "Contrato": null
        }
    ]
}

Model Model

public partial class PositionsModel
    {
        [JsonProperty("$id")]
        public long Id { get; set; }

        [JsonProperty("$values")]
        public posicionesInfo[] portafolio { get; set; }
    }

    public partial class posicionesInfo
    {
        [JsonProperty("$id")]
        public long Id { get; set; }

        [JsonProperty("Contrato_Id")]
        public string ContratoId { get; set; }

        [JsonProperty("Emision_Id")]
        public string EmisionId { get; set; }

        [JsonProperty("Tipo_Custodia_Id")]
        public long TipoCustodiaId { get; set; }

        [JsonProperty("Institucion_Deposito_Id")]
        public long InstitucionDepositoId { get; set; }

        [JsonProperty("Portafolio_Tit_Imp")]
        public double PortafolioTitImp { get; set; }

        [JsonProperty("Portafolio_Costo_Promedio")]
        public double PortafolioCostoPromedio { get; set; }

        [JsonProperty("Portafolio_Comprometido")]
        public long PortafolioComprometido { get; set; }

        [JsonProperty("Portafolio_TIR")]
        public long PortafolioTir { get; set; }

        [JsonProperty("Portafolio_Plazo")]
        public long PortafolioPlazo { get; set; }

        [JsonProperty("Contrato")]
        public object Contrato { get; set; }
    }

ViewModel视图模型

private async void GetDataAsync()
        {
            HttpClient httpClient = new HttpClient();
            var response = await httpClient.GetAsync("http://192.168.1.69/ApiTest/Api/Portafolio/FRANOPRA");

            if (response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync();
                //Error on deserializer
                var positions = JsonConvert.DeserializeObject<PositionsModel>(content);
                portafolioInfo = new List<PositionsModel>((IEnumerable<PositionsModel>)positions);
            }
        }

The problem is $ in keys, because it's indicates metadata, not an actual data field, so you need to ignore it, to get the expected result, like the following code:问题是键中的$ ,因为它表示元数据,而不是实际的数据字段,所以你需要忽略它,以获得预期的结果,如以下代码:

JsonSerializerSettings settings = new JsonSerializerSettings
{
    MetadataPropertyHandling = MetadataPropertyHandling.Ignore
};

var position = JsonConvert.DeserializeObject<PositionsModel>(json, settings);
List<PositionsModel> portafolioInfo = new List<PositionsModel> { position };

Fiddle: https://dotnetfiddle.net/oVtjDa小提琴: https://dotnetfiddle.net/oVtjDa

I hope you find this helpful.我希望你觉得这有帮助。

暂无
暂无

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

相关问题 无法反序列化当前 JSON object 因为该类型需要 JSON 数组(例如 [1,2,3])才能正确反序列化 - Cannot deserialize the current JSON object because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly 如何修复“无法将当前的 JSON 数组反序列化为“段落”类型,因为需要 JSON object(例如 {“name”:“值”) - How to fix 'Cannot deserialize the current JSON array into type 'passages' because requires a JSON object (e.g. {“name”:“value”}) to deserialize 无法将当前JSON对象反序列化为类型&#39;WindowsService1 ...需要JSON数组(例如[1,2,3])才能正确反序列化 - Cannot deserialize the current JSON object into type 'WindowsService1…requires a JSON array (e.g. [1,2,3]) to deserialize correctly 无法将当前 JSON object(例如 {“name”:“value”})反序列化为类型需要 JSON 数组(例如 [1,2,3] 才能正确反序列化) - Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type requires a JSON array (e.g. [1,2,3]) to deserialize correctly 无法将当前 JSON 对象反序列化为类型“System.Collections.Generic.List”,因为该类型需要 JSON 数组才能正确反序列化 - Cannot deserialize the current JSON objectinto type 'System.Collections.Generic.List because the type requires a JSON arrayto deserialize correctly 无法将当前JSON数组(例如[1,2,3])反序列化为类型”,因为该类型需要JSON对象(例如{“ name”:“ value”}) - Cannot deserialize the current JSON array (e.g. [1,2,3]) into type '' because the type requires a JSON object (e.g. {“name”:“value”}) 无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型“ConsoleAppTest01.Location”,因为该类型需要 JSON ZA8CFDE6331BD59EB2AC96F8911ZC4 - Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'ConsoleAppTest01.Location' because the type requires a JSON object 无法将 JSON 数组(例如 [1,2,3])反序列化为类型“ ”,因为类型需要 JSON 对象(例如 {&quot;name&quot;:&quot;value&quot;})才能正确反序列化 - Cannot deserialize the JSON array (e.g. [1,2,3]) into type ' ' because type requires JSON object (e.g. {"name":"value"}) to deserialize correctly 无法将 JSON 数组(例如 [1,2,3])反序列化为类型“”,因为类型需要 JSON object(例如,将 {“name”} 正确地反序列化为“value”: - Cannot deserialize the JSON array (e.g. [1,2,3]) into type ' ' because type requires JSON object (e.g. {“name”:“value”}) to deserialize correctly Twitter:无法将JSON数组反序列化为对象,因为该类型需要JSON对象 - Twitter : Cannot deserialize a JSON array into an object because the type requires a JSON object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM