C#中的Json反序列化异常 [英] Json deserialisation Exception in C#

查看:667
本文介绍了C#中的Json反序列化异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Studio 2013

我的杰森(Json)看上去像

 {
  代码":0,
  消息":成功",
  组织":[
    {
"a":"b";

"c":"d";
     },
     {

     }]
}


我使用下面的代码对Json进行了序列化

 var groups = response.Content.ReadAsAsync< IEnumerable< Organization>>().Result; 

但是我遇到了诸如

 Newtonsoft之类的异常.Json.JsonSerializationException:无法反序列化当前JSON
对象(例如{"name":"value"})转换为类型'System.Collections.Generic.IEnumerable
"1 [OrganizationApi.Organization]",因为该类型需要JSON数组(例如[
1,2,3])正确反序列化.
要解决此错误,请将JSON更改为JSON数组(例如[1,2,3])或更改
e反序列化类型,以便它是普通的.NET类型(例如,不是原始t
ype像整数,而不是像数组或List< T>)这样的集合类型
从JSON对象序列化.也可以将JsonObjectAttribute添加到类型中
 强制其从JSON对象反序列化.
路径代码",第1行,位置8.
   在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(Js
onReader阅读器,类型objectType,JsonContract合同,JsonProperty成员,Js
onContainerContract containerContract,JsonProperty containerMember,对象exis
tingValue)
   在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInte
rnal(JsonReader reader,Type objectType,JsonContract contract,JsonProperty mem
ber,JsonContainerContract containerContract,JsonProperty containerMember,Obje
ct existingValue)
   在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(Jso
nReader reader,类型为objectType,布尔值checkAdditionalContent)
   在Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader,Type
 objectType)
   在Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader,Type objectT
是的
   在System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.ReadFromStream(Type
类型,流readStream,编码有效编码,IFormatterLogger formatterL
ogger)
   在System.Net.Http.Formatting.JsonMediaTypeFormatter.ReadFromStream(Type type
,流readStream,编码有效编码,IFormatterLogger formatterLogge
r)
   在System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.ReadFromStream(Type
类型,流readStream,HttpContent内容,IFormatterLogger formatterLogger)
   在System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.ReadFromStreamAsync(
类型类型,流readStream,HttpContent内容,IFormatterLogger formatterLog
格)
---从上一个引发异常的位置开始的堆栈结束跟踪---
   在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
   在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot
净化(任务任务)
   在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()处
   请参阅System.Net.Http.HttpContentExtensions.< ReadAsAsyncCore&d; d__0`1.MoveNext()

请帮助我解决此问题

解决方案

 

{   代码":  0,  消息":成功",   组织":  [     { " a":"b" ″ c″:″ d″.     },      {     } }

在json中使用,",而不是;".


I'm using the visual studio 2013

my Json looks like as

{
  "code": 0,
  "message": "success",
  "organizations": [
    {
	"a":"b";

	"c":"d";
     },
     {

     }]
}


I used the below line to de serialize that Json

 var organizations = response.Content.ReadAsAsync<IEnumerable<Organization>>().Result;

but i'm getting the exception such as

Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON
object (e.g. {"name":"value"}) into type 'System.Collections.Generic.IEnumerable
`1[OrganizationApi.Organization]' because the type requires a JSON array (e.g. [
1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or chang
e the deserialized type so that it is a normal .NET type (e.g. not a primitive t
ype like integer, not a collection type like an array or List<T>) that can be de
serialized from a JSON object. JsonObjectAttribute can also be added to the type
 to force it to deserialize from a JSON object.
Path 'code', line 1, position 8.
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(Js
onReader reader, Type objectType, JsonContract contract, JsonProperty member, Js
onContainerContract containerContract, JsonProperty containerMember, Object exis
tingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInte
rnal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty mem
ber, JsonContainerContract containerContract, JsonProperty containerMember, Obje
ct existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(Jso
nReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type
 objectType)
   at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectT
ype)
   at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.ReadFromStream(Type
type, Stream readStream, Encoding effectiveEncoding, IFormatterLogger formatterL
ogger)
   at System.Net.Http.Formatting.JsonMediaTypeFormatter.ReadFromStream(Type type
, Stream readStream, Encoding effectiveEncoding, IFormatterLogger formatterLogge
r)
   at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.ReadFromStream(Type
type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
   at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.ReadFromStreamAsync(
Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLog
ger)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot
ification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at System.Net.Http.HttpContentExtensions.<ReadAsAsyncCore>d__0`1.MoveNext()

please help me in resolving this issue

解决方案

{   "code": 0,   "message": "success",   "organizations": [     { "a":"b", "c":"d"      },      {      }] }

Use ',' in your json, rather that ';'.


这篇关于C#中的Json反序列化异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆