本文介绍了如何序列化的MonoTouch使用System.Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题似乎过于宽泛,因此我因子评分我创建在细节上新的。很抱歉,如果这被认为是不好的做法。

My other question seems to be too generic, so I tought I'd create a new one on the details. Sorry if that is considered bad practice.

我想序列化的C#对象JSON字符串与MonoTouch的和不需要我的方式System.Json命名空间通过对象(S)自己要下降。那可能吗?如果是,如何正确地做呢?

I am trying to serialize C# objects to JSON strings with MonoTouch and the System.Json namespace in a way that doesn't require me to descend through the object(s) myself. Is that possible? If yes, how to do it properly?

反序列化通过隐含铸造JsonValue为字符串,INT,不管效果很好。此外,在层次结构降是没有问题的。像这样:

De-serialization works well by implicitly casting a JsonValue to a string, int, whatever. Also, descending in the hierarchy is no problem. Like so:

JsonValue json = JsonValue.Parse(jsonString);
int mainValue = json["mainValue"];

JsonValue subValues = json["subValues"];
int subValue1 = subValues["subValue1"];



相反的是只与元素类型​​的可能(串/ INT / ...)。其他对象不能转换为JsonValue / JSONObject的不幸。甚至非常简单结构只有两个整数。

The opposite is only possible with elemental types (string/int/...). Other objects cannot be cast to JsonValue/JsonObject unfortunately. Not even really simple structs with just two ints.

// This works
JsonValue json = new JsonObject(new KeyValuePair<string,JsonValue>("mainValue", 12345));

// Cannot (implicitly) convert type 'MyObjectType' to 'System.Json.JsonValue'
MyObjectType myObject = new MyObjectType();
JsonValue subValues = myObject;

// Cannot (implicitly) convert type 'MySimpleStruct' to 'System.Json.JsonObject'
MySimpleStruct myStruct;
myStruct.x = 1;
myStruct.y = 2;
JsonValue myStructJson = myStruct;



由于我的对象嵌套在其他对象的几个层次,由我通过它行走和分配所有值将是一个伟大PITA。是否与System.Json一个更简单的方法?

As my object has several levels of other objects nested within, walking through it by myself and assigning all the values would be a great PITA. Is there a simpler way with System.Json?

推荐答案

System.Json不支持这样的任意序列化。

System.Json doesn't support arbitrary serialization like this.

您可以使用第三方JSON库像Newtonsoft,或等待MonoTouch的V4,这将有DataContractJsonSerializer

You can use a third party Json library like Newtonsoft, or wait for MonoTouch v4 which will have the DataContractJsonSerializer

这篇关于如何序列化的MonoTouch使用System.Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 12:14