How to generate JSON programmatically using JSON framework for iPhone
Create an array or dictionary of objects representing the information you want to send via JSON. Having done that, send -JSONRepresentation
to the array/dictionary. That method returns a JSON string, and you send it to the server.
For instance:
NSDictionary *o1 = [NSDictionary dictionaryWithObjectsAndKeys:
@"some value", @"key1",
@"another value", @"key2",
nil];
NSDictionary *o2 = [NSDictionary dictionaryWithObjectsAndKeys:
@"yet another value", @"key1",
@"some other value", @"key2",
nil];
NSArray *array = [NSArray arrayWithObjects:o1, o2, nil];
NSString *jsonString = [array JSONRepresentation];
// send jsonString to the server
After executing the code above, jsonString
contains:
[
{
"key1": "some value",
"key2": "another value"
},
{
"key1": "yet another value",
"key2": "some other value"
}
]