使用JSON Serializer的SerializationException [英] SerializationException using JSON Serializer

查看:68
本文介绍了使用JSON Serializer的SerializationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我反序列化一个用JSON序列化的简单对象时,我收到一个带有以下消息的SerializationException:

I'm getting a SerializationException with the following message when I deserialize a simple object serialized with JSON:

期望来自命名空间''的元素'root'。遇到'无' 名称'',名称空间''。

任何想法有什么不对? 以下是代码:

Any ideas what's wrong?  Here is the code:

namespace

namespace

 

 

JSONSerialization

JSONSerialization

{

 

 

class
程序

class Program

{

[

  ;

DataContract
]

DataContract ]

 

 

public
class
信息

public class Info

{

[

  ;

DataMember
]

DataMember ]

 

 

public
string
版本;

public string version;

}

 

 

静态
void
Main(
string
[] args)

static void Main( string [] args)

{

 

 

信息
apiInfo =
new
信息
(){version =
" 1.0"
};

Info apiInfo = new Info () {version = "1.0" };

 

 

MemoryStream
stream1 =
new
MemoryStream
();

MemoryStream stream1 = new MemoryStream ();

 

 

DataContractJsonSerializer
ser =
new
DataContractJsonSerializer

typeof

信息
) );

DataContractJsonSerializer ser = new DataContractJsonSerializer ( typeof ( Info ));

ser.WriteObject(stream1,apiInfo

ser.WriteObject(stream1, apiInfo

 

);

);

 

 

信息
proxyReceived =(
< span style ="font-family:Consolas;颜色:#2b91af; font-size:x-small"> 信息
)ser.ReadObject(stream1);

Info proxyReceived = ( Info )ser.ReadObject(stream1);

 

 

控制台
。WriteLine(
< span style ="font-family:Consolas; color:#0000ff; font-size:x-small"> string
。格式(
" {0 }:{1}"

" API版本"
,proxyReceived.version));

Console .WriteLine( string .Format( "{0}: {1}" , "API version" , proxyReceived.version));

}

}

}

推荐答案

我想我最近打了这个,是不是你没有将你的内存流重置到开头,所以它试图从流的末尾读取?

Tim
I think I hit this recently, is it that you didn't reset your memory stream to the beginning, so it is trying to read from the end of the stream?
Tim


这篇关于使用JSON Serializer的SerializationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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