0

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

Sebastien Peek
  • 2,528
  • 2
  • 23
  • 32
jim
  • 8,670
  • 15
  • 78
  • 149
  • Instead of creating a dictionary **for each** name-value pair, create a dictionary that contains **all** name-value pairs of a given object. In your example, it’d be one dictionary containing two name-value pairs. –  Jul 05 '11 at 19:23

2 Answers2

4

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]];
JeremyP
  • 84,577
  • 15
  • 123
  • 161
1

Check my answer to the '' SBJsonWriter Nested NSDictionary '' question. it illustrates how to properly use SBJsonWriter.

It includes error check and some pieces of advise about SBJsonWriter behaviour with NSDate, float, etc..

Excerpt:

NSDictionary* aNestedObject = [NSDictionary dictionaryWithObjectsAndKeys:
                                      @"nestedStringValue", @"aStringInNestedObject",
                                      [NSNumber numberWithInt:1], @"aNumberInNestedObject",
                                 nil];

NSArray * aJSonArray = [[NSArray alloc] initWithObjects: @"arrayItem1", @"arrayItem2", @"arrayItem3", nil];

NSDictionary * jsonTestDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                     @"stringValue", @"aString",
                                     [NSNumber numberWithInt:1], @"aNumber",
                                     [NSNumber numberWithFloat:1.2345f], @"aFloat",
                                     [[NSDate date] description], @"aDate",
                                     aNestedObject, @"nestedObject",
                                     aJSonArray, @"aJSonArray",
                                     nil];
Community
  • 1
  • 1
Pascal
  • 15,257
  • 2
  • 52
  • 65