JSON反序列化与MonoTouch的 [英] JSON de-serialization with MonoTouch

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

问题描述

有网络解释如何序列一些C#对象一个JSON字符串各种ressources。我一直无法得到任何人是的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由詹姆斯·牛顿景结果
我设法让通过包括.NET 2.0程序集DLL,并使用Newtonsoft.Json.JsonConvert.SerializeObject在模拟器上这个工作()。但是,这并不编译错误设备的的mTouch没有输出失败。当我删除了集引用,它编译罚款。

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

下面是由指数的例子:

Here is an example by index:

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



我的一个名称:

One my name:

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



其中的这个的是一个名为的StringValue的字符串属性和一个int类属性命名INTVALUE。在大多数情况下,该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.

在这个问题上,读到这里就保留更多的信息:的 MonoTouch的文件

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

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

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