简体   繁体   English

反序列化字符串时出现 JsonConvert.DeserializeObject 错误

[英]JsonConvert.DeserializeObject error while deserializing a string

I am trying to deserialize a json string that is coming from a js code, the data holds the json string that I want to deserialize.我正在尝试反序列化来自 js 代码的 json 字符串,数据包含我想要反序列化的 json 字符串。

The coming string is:即将到来的字符串是:

{"metalRingID":"FB11111","birdData":[{"longitude":-3.0851070084624497,"latitude":51.02832183751735,"gridRef":"ST2426","date":"2020-01-05T00:00:00"},{"longitude":-2.233409881591797,"latitude":51.5276985168457,"gridRef":null,"date":"2020-01-02T00:00:00"},{"longitude":-2.3790299892425537,"latitude":51.4547004699707,"gridRef":null,"date":"2020-01-03T00:00:00"},{"longitude":-1.6884700059890747,"latitude":51.68299865722656,"gridRef":null,"date":"2020-01-05T00:00:00"}]}

This is the model to hold that data:这是保存该数据的模型:

using System;
using Newtonsoft.Json;

namespace BirdProject.Model.ViewModel
{
    public class birdDataSolutionVM
    {
        [JsonProperty("metalRingID")]
        public string metalRingID;

        [JsonProperty("birdData")]
        public List<birdRecordVM> birdData;
    }
}

This is the line that should do the job.这是应该完成这项工作的生产线。

var birdRecords = JsonConvert.DeserializeObject<List<birdDataSolutionVM>>(data);

The error I am receiving is the next:我收到的错误是下一个:

Cannot deserialize the current JSON object (eg {"name":"value"}) into type 'System.Collections.Generic.List`1[BirdProject.Model.ViewModel.birdDataSolutionVM]' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly.\nTo 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 对象(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List`1[BirdProject.Model.ViewModel.birdDataSolutionVM]' 因为该类型需要 JSON 数组(例如 [ 1,2,3]) 以正确反序列化。\n要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或将反序列化类型更改为正常的 .NET 类型(例如不是像整数这样的原始类型,不是像数组或列表这样的集合类型)可以从 JSON 对象反序列化。 JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.\nPath 'metalRingID', line 1, position 15. JsonObjectAttribute 也可以添加到该类型以强制它从 JSON 对象反序列化。\n路径“metalRingID”,第 1 行,位置 15。

You should be deserializing into the type, not an array of the type.您应该反序列化为类型,而不是类型的数组。 It's a single object containing an array, but the root json is not an array.它是包含数组的单个对象,但根 json 不是数组。

var birdRecords = JsonConvert.DeserializeObject<birdDataSolutionVM>(data);

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

相关问题 反序列化为数据集时,JsonConvert.DeserializeObject 不起作用 - JsonConvert.DeserializeObject is not working when deserializing to a DataSet 使用JsonConvert.DeserializeObject时出错 - Error using JsonConvert.DeserializeObject 转换JSON字符串C#时出现JsonConvert.DeserializeObject错误 - JsonConvert.DeserializeObject error when converting JSON string c# 使用 JsonConvert.DeserializeObject 在 c# 中反序列化多维 json 对象时出现问题 - Trouble while deserializing multidimentional json object in c# using JsonConvert.DeserializeObject 为什么JsonConvert.DeserializeObject会更改String值? - Why JsonConvert.DeserializeObject change the String value? JsonConvert.DeserializeObject()无法正确反序列化字符串 - JsonConvert.DeserializeObject() cannot deserialize the string properly JsonConvert.DeserializeObject读取对象引用&#39;1&#39;时出错 - JsonConvert.DeserializeObject Error reading object reference '1' JsonConvert.DeserializeObject错误将值转换为类型 - JsonConvert.DeserializeObject error converting value to type 在JsonConvert.DeserializeObject中反序列化对象时出现意外的标记 - Unexpected token when deserializing object in JsonConvert.DeserializeObject JsonConvert.DeserializeObject 在反序列化 CouchBase 响应时抛出异常 - JsonConvert.DeserializeObject throws exception when deserializing CouchBase response
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM