Windows Phone的Json序列化 [英] Json Serialization for Windows Phone

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

问题描述

我正在尝试实现JSON响应的解析,如此处所示我的Windows Phone 7项目在C#中.但是我陷入了编译错误,因为找不到类型或名称空间名称'Serializable'(您是否缺少using指令或程序集引用?")

I was trying to implement parsing a JSON response as shown here for my Windows Phone 7 project in C#. But I am stuck with a compilation error as "The type or namespace name 'Serializable' could not be found (are you missing a using directive or an assembly reference?)"

我使用System.Runtime.Serialization进行了导入;使用System.Runtime.Serialization.Json;我不确定我缺少什么.我试图包括使用System.ServiceModel.Web;但是Web部分未被识别.

I have the imports using System.Runtime.Serialization; using System.Runtime.Serialization.Json; I am not sure what are import I am missing. I tried to include using System.ServiceModel.Web; But the Web part is not recognized.

我认为我的项目没有从这里.但是在大会信息中,我没有选择更改目标框架的方法.

I thought my project is not pointing to the right framework from here. But in the Assembly information, there is no option for me to change the target framework.

看起来像是我的类似问题,但我无法在.net dlls中找不到为Windows Phone过滤的JSON.NET.

This looks like a similar problem to mine, but I couldn't find the JSON.NET in .net dlls which is filtered for Windows Phone.

有人可以帮我得到适用于Windows Phone 7的JSON吗.

Can someone help me to get this JSON thing working for Windows Phone 7.

预先感谢.

编辑-7/3/11

我的Jason回应是{"serviceresponse":{"servicename":"RequestRegisterUser",.....

My Jason response is { "serviceresponse" : { "servicename" : "RequestRegisterUser", .....

我的Response对象是:

And my Response objects are:

[DataContract]
       public class serviceresponse
       {
             [DataMember]
           public String servicename { get; set; }
           .
           .
           .

还有我的反序列化方法:

And my Deserialize method:

 public static T Deserialise<T>(string json)
   {
       T obj = Activator.CreateInstance<T>();
       using (MemoryStream stream = new MemoryStream(Encoding.Unicode.GetBytes(json)))
       {
           DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
           obj = (T)serializer.ReadObject(stream);
           return obj;
       }
   }

现在反序列化响应后出现此错误:servicename无法计算表达式字符串

Now I am getting this error after Deserializing the response: servicename Could not evaluate expression string

(尽管引用中包含dll,但我无法导入System.ServiceModel.Web..Web部件上出现编译错误(名称空间"System.ServiceModel"中不存在类型或名称空间名称"Web"))

( I could not import System.ServiceModel.Web though I have the dll in the reference. A compilation error on the .Web part (The type or namespace name 'Web' does not exist in the namespace 'System.ServiceModel') )

编辑经过更多研究,我发现我在调试器中查看时的反应实际上是{\"serviceresponse \":{\"servicename \":\"RequestRegisterUser \",...我进行了搜索,发现这可能是一个问题.如何格式化它以更正JSON字符串?

EDIT After more research, I found my response when viewed in the debugger is actually { \"serviceresponse\": { \"servicename\": \"RequestRegisterUser\",..... I searched for this and found this could be a problem. How can I format it to correct JSON String??

推荐答案

我发现了问题.尽管我的类名是"serviceresponse",但我还是使用了另一个包装器类

I found the issue. Though my class name is "serviceresponse", I used another wrapper class as

public class Response
{
   public serviceresponse serviceres;//Don't Do this....
}

我在其中使用变量名称作为serviceresponse的serviceresponse.但是当我将其更改为"serviceresponse"时,一切正常.

where I used the variable name for serviceresponse as serviceres. But when I changed it to " serviceresponse" its all working.

public class Response
{
   public serviceresponse serviceresponse;//This fixed it.
}

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

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