码迷,mamicode.com
首页 > Web开发 > 详细

Newtonsoft.Json 序列化

时间:2015-07-12 06:59:48      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:

当我们对一个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         }

 

Newtonsoft.Json 序列化

标签:

原文地址:http://www.cnblogs.com/xiao-sheng/p/4639923.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!