如何在monotouch(4.0)中手动创建JSON对象?
System.JSON命名空间可用,(JsonObject
,JsonArray
,jSonPrimitive
和JsonValue
)但这些都是抽象的,所以我不能这样做:
JsonObject myObject = new JsonObject();
我需要手动构建一个JSON对象,并且不想使用DataContractSerialisation。
供参考:
- 我需要稍后将该JSON对象传输到具有Web调用的服务器。 (但我可以处理那部分)
答案 0 :(得分:3)
使用JSON.net http://json.codeplex.com/
答案 1 :(得分:1)
事实证明,经过长时间的尝试和搜索;只能使用JsonPrimitive
构造函数和JsonObject
构造函数。
JsonPrimitive
和JsonValue
都可以投放到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));