• Newtonsoft.Json 序列化


    当我们对一个json数组进行反序列化用Newtonsoft.Json.JsonConvert.DeserializeObject<T>() 通常会报此错误

    Newtonsoft.Json.dll 中发生,但未在用户代码中进行处理

    其他信息: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'MyHttp.Controllers.ValuesController+ProvinceModel' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

    To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.

    Path '', line 1, position 1.

     1      public void PostProvince()
     2         {
     3             var retString = "[{"ProvinceName":"湖北省"},{"ProvinceName":"河北省"}]";
     4             var provinces = Newtonsoft.Json.JsonConvert.DeserializeObject<ProvinceModel>(retString);
     5         }
     6 
     7 
     8         public class ProvinceModel
     9         {
    10             public string ProvinceName { get; set; }13         }

    解决方案

    1      public void PostProvince()
    2         {
    3             var retString = "[{"ProvinceName":"湖北省"},{"ProvinceName":"河北省"}]";
    4             var provinces = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ProvinceModel>>(retString);
    5         }
  • 相关阅读:
    剑指 Offer 33. 二叉搜索树的后序遍历序列
    剑指 Offer 32
    OCR技术浅析-tesserOCR(3)
    OCR技术浅析-自写篇(2)
    OCR技术浅析-无代码篇(1)
    SVN服务器+客户端安装和配置
    Git命令集
    数组模拟队列与单调队列求解滑动窗口
    upper_bound与lower_bound
    图中连通块的划分
  • 原文地址:https://www.cnblogs.com/xiao-sheng/p/4639923.html
Copyright © 2020-2023  润新知