【问题标题】:Cannot deserialize the current JSON object into type 'System.Collections.Generic.List because the type requires a JSON array to deserialize correctly无法将当前 JSON 对象反序列化为“System.Collections.Generic.List”类型,因为该类型需要 JSON 数组才能正确反序列化
【发布时间】:2020-04-17 03:22:43
【问题描述】:

无法反序列化当前的 JSON 对象

例如{"name":"value"} 输入类型

System.Collections.Generic.List`1[domvc.Models.EMP]

因为该类型需要 JSON 数组(例如 [1,2,3])才能正确反序列化。

【问题讨论】:

  • 请添加示例 json 格式 + EMP 类详细信息,所以有问题

标签: .net


【解决方案1】:

我遇到了同样的错误。问题是要处理的 JSON 的数组定义。

创建主数据类Routine后,创建对象获取列表

public class RoutineList
{
    public List<Routine> routines { get; set; }
}

然后读取JSON并像反序列化

RoutineList rtns = new RoutineList();
rtns = JsonConvert.DeserializeObject<RoutineList>(result);

JSON 知道反序列化的数据对应于Routines 的列表,在这种情况下。这为我解决了错误。

【讨论】:

    猜你喜欢
    • 2021-10-29
    • 2020-11-15
    • 2019-03-18
    • 2022-06-11
    • 1970-01-01
    • 1970-01-01
    • 2021-06-22
    相关资源
    最近更新 更多