Newtonsoft.Json 反序列化中时间格式的问题

.Net技术 码拜 9年前 (2015-07-18) 1617次浏览 0个评论
 

我在生成json字符串的时候 使用自定义的时间格式

   IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();           
            timeConverter.DateTimeFormat = "yyyy""-""MM""-""dd"" ""HH"":""mm"":""ss";
            return JsonConvert.SerializeObject(obj, Formatting.None, timeConverter);

然后我在反序列化的时候悲剧了

            IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();            
             timeConverter.DateTimeFormat = "yyyy""-""MM""-""dd"" ""HH"":""mm"":""ss";
            return JsonConvert.DeserializeObject(jsonstr, type, timeConverter);

这时候 老是说我时间格式错误,  我要怎么办?

20分

#1

引号里面,不应该有单引号了

“yyyy-MM-dd HH:mm:ss”

#2

仔细看了下 好像又不是时间格式的问题
错误信息
“Newtonsoft.Json.JsonSerializationException”类型的异常在 Newtonsoft.Json.dll 中发生,但未在用户代码中进行处理

其他信息: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type “”Model.UserTransactionRecords_MD”” 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.

数据

{“id”:1,”msg”:null,”data”:[{“TradeID”:12,”CreateTime”:”2015-06-09 00:00:00″,”Stockcount”:1000,”OperationType”:2,”UnitPrice”:53.95,”StockCode”:”000777″,”TotalMoney”:53966.185,”TaxMoney”:0.0,”IncidentalsMoney”:0.0,”CommissionMoney”:16.185,”TableName”:”UserTransactionRecords”},{“TradeID”:13,”CreateTime”:”2015-06-09 00:00:00″,”Stockcount”:1000,”OperationType”:2,”UnitPrice”:53.82,”StockCode”:”000777″,”TotalMoney”:53836.146,”TaxMoney”:0.0,”IncidentalsMoney”:0.0,”CommissionMoney”:16.146,”TableName”:”UserTransactionRecords”}]}

20分

#3

既然你是用自定义的时间格式,那么它其实根本不是时间类型,而是个格式化字符串而已
你反序列化的时候,也要用字符串类型来接啊

#4

错误信息 露了 一句   Path “”data””, line 1, position 27.

#5

晕了   , 我错了  把 List<> 漏掉了

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Newtonsoft.Json 反序列化中时间格式的问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!