objective-cjsonsbjson

Objective-c SBJSONWriter convert array of dictionaries to JSON


I'm having some trouble with SBJsonWriter at the moment.

I need to send a request that contains a json object of name/value pairs. e.g.

[{%22uid%22:1,%22version%22:1}]

I can't figure out how to do this in Obj-C with the SBJson Writer framework.

For each pair I have tried to construct a dictionary then add the dictionary to an array. This results in an array containing many dictionaries, each containing one name/value pair.

Any idea on how a fix for this or is it possible?

Thanks in advance


Solution

  • To produce an Objective-C structure equivalent to the above JSON you should do this:

    NSArray* json = [NSArray arrayWithObject: [NSDictionary dictionaryWithObjectsAndKeys: 
                                                   [NSNumber numberWithInt: 1], @"uid",
                                                   [NSNumber numberWithInt: 1], @"version",
                                                   nil]];