Timothy Groote
Timothy Groote

Reputation: 8643

Creating a JSON object in MonoTouch (C#)

How do i manually create a JSON object in monotouch(4.0)?

The System.JSON namespace is available, (JsonObject, JsonArray, jSonPrimitive and JsonValue) but those are all abstract, so i can't just do this :

JsonObject myObject = new JsonObject();

I need to manually build a JSON object, and do not want to use DataContractSerialisation.

For reference purposes :

-I will need to transfer that JSON object to a server with a web call later. (but i can handle that part)

Upvotes: 1

Views: 1123

Answers (2)

Timothy Groote
Timothy Groote

Reputation: 8643

As it turns out, after a long time of trying and searching ; only the JsonPrimitiveconstructor and the JsonObject constructor can be used.

JsonPrimitive and JsonValue can both be cast to JsonValue.

the JsonObject requires a KeyValuePair<string, JsonValue>

if i define functions like this :

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

i can create a json object like this :

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

Upvotes: 1

Andrew Young
Andrew Young

Reputation: 1779

Use JSON.net http://json.codeplex.com/

Upvotes: 3

Related Questions