使用 MonoTouch 进​​行 JSON 反序列化 [英] JSON de-serialization with MonoTouch

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

问题描述

网络上有各种资源解释了如何将某些 C# 对象序列化为 JSON 字符串.但是,我无法让它们中的任何一个与 MonoTouch 3.2.4 一起使用.

there are various ressources on the web explaining how to serialize some C# object to a JSON string. I have been unable to get any of them to work with MonoTouch 3.2.4 though.

我遇到的替代方案:

  • System.Json 命名空间
    我已经成功地使用 System.Json 命名空间来反序列化数据.因此,为简单起见,如果可能的话,我更愿意坚持这一点.不幸的是,我不知道如何使用它进行序列化(或者甚至可能).

  • the System.Json Namespace
    I am already successfully using the System.Json namespace to de-serialize data. Hence, for simplicity's sake, I'd prefer to stick to that if possible. Unfortunately, I have no idea how to serialize with it (or if it is even possible).

System.Runtime.Serialization.Json.DataContractJsonSerializer
System.Runtime.Serialization.Json 命名空间在 MonoTouch 中不可用.我在某处读到它是 .NET 3.5 的一部分,而 MonoTouch 处于 .NET 2.0 级别.这或许可以解释.

System.Runtime.Serialization.Json.DataContractJsonSerializer
The System.Runtime.Serialization.Json namespace is not available in MonoTouch. I've read somewhere that it is part of .NET 3.5 and that MonoTouch is at .NET 2.0 level. That might explain it.

System.Web.Script.Serialization.JavaScriptSerializer
MonoTouch 中也缺少这个(实际上,似乎只有 System.Web.Services 可用).

System.Web.Script.Serialization.JavaScriptSerializer
This one is also missing in MonoTouch (in fact, there only seems to be System.Web.Services available).

Json.NET by James Newton-King
我设法通过包含 .NET 2.0 程序集 dll 并使用 Newtonsoft.Json.JsonConvert.SerializeObject() 使这个在模拟器上工作.但是,这不会为带有错误mtouch failed with no output"的设备编译.一旦我删除了程序集引用,它就可以正常编译.

Json.NET by James Newton-King
I managed to make this one work on the simulator by including the .NET 2.0 assembly dll and using Newtonsoft.Json.JsonConvert.SerializeObject(). However, this doesn't compile for the device with error "mtouch failed with no output". As soon as I remove the assembly reference, it compiles fine.

有什么方法可以使其中一个与 MonoTouch 一起使用?还有其他替代方案可以尝试吗?

Is there any way to make one of those work with MonoTouch? Are there other alternatives to try?

谢谢

推荐答案

我建议使用 System.Json 库,该库使用起来并不太难,并且包装在一个类中以使其成为强类型.它也作为默认程序集包含在 MonoTouch 中,因为它继承自 Silverlight.

I'd recommend using the System.Json library, which is not too difficult to use and wrap in a class to make it strongly typed. It is also included as a default assembly with MonoTouch as it was inherited from Silverlight.

加载:

JsonValue value = JsonObject.Load(stream); //There are other overloads here, my stream is off an HttpWebRequest

这是一个索引示例:

this.StringValue = value[0];
this.IntValue = value[1];

一个我的名字:

this.StringValue = value["StringValue"];
this.IntValue = value["IntValue"];

其中 this 是一个具有名为 StringValue 的字符串属性和名为 IntValue 的 int 属性的类.在大多数情况下,JsonValue 类将隐式转换为您的类型.

Where this is a class with a string property named StringValue and an int property named IntValue. For the most part, the JsonValue class will cast to your types implicitly.

所以我要做的是创建一个类来以强类型的方式保存所有的 Json 信息,然后在构造函数中传入一个 JsonValue 来做丑陋的弱类型的东西.

So what I do, is create a class to hold all of the Json info in a strongly typed way, then pass in a JsonValue in the constructor to do the ugly weakly typed stuff.

对于其他库,您会遇到一些问题,即 MonoTouch 会删除通过反射创建时被视为未使用"的类型(例如,当您使用 XmlSerializer 时).我发现自己在解决这个问题时遇到了不必要的问题,不得不在类型等上添加 PreserveAttribute.

For other libraries, you will run into problems where MonoTouch strips out types that are seen to be "unused" when created by reflection (as when you use XmlSerializer, for example). I have found myself fighting that issue more than necessary, having to add PreserveAttribute on types, etc.

关于此问题,请阅读此处了解有关 Preserve 的更多信息:MonoTouch Doc

On this issue, read here for more info on Preserve: MonoTouch Doc

这篇关于使用 MonoTouch 进​​行 JSON 反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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