在MonoTouch中创建JSON对象(C#)

时间:2011-04-29 13:06:59

标签: json c#-4.0 mono xamarin.ios

如何在monotouch(4.0)中手动创建JSON对象?

System.JSON命名空间可用,(JsonObjectJsonArrayjSonPrimitiveJsonValue)但这些都是抽象的,所以我不能这样做:

JsonObject myObject = new JsonObject();

我需要手动构建一个JSON对象,并且不想使用DataContractSerialisation。

供参考:

- 我需要稍后将该JSON对象传输到具有Web调用的服务器。 (但我可以处理那部分)

2 个答案:

答案 0 :(得分:3)

使用JSON.net http://json.codeplex.com/

答案 1 :(得分:1)

事实证明,经过长时间的尝试和搜索;只能使用JsonPrimitive构造函数和JsonObject构造函数。

JsonPrimitiveJsonValue都可以投放到JsonValue

JsonObject需要KeyValuePair<string, JsonValue>

如果我定义这样的函数:

public static KeyValuePair<String, JsonValue>  IntRequired(string key, int val)
{
    return new KeyValuePair<String, JsonValue>(key, new JsonPrimitive(val));
}

我可以像这样创建一个json对象:

JSonObject myObject = new JsonObject();
myObject.add(new IntRequired("id",1));