Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
928 views
in Technique[技术] by (71.8m points)

json - SBJsonWriter Nested NSDictionary

I'm trying to serialize an objc object with Json to send it to a server.

That same server sends the following on GET for this object type:

 {
   "TypeProperties":[
      {"Key":"prop 0","Value":"blah 0"},
      {"Key":"prop 1","Value":"blah 1"},
      {"Key":"prop 2","Value":"blah 2"},
      {"Key":"prop 3","Value":"blah 3"}
     ],
   "ImageURls":[
      {"Key":"url 0","Value":"blah 0"},
      {"Key":"url 1","Value":"blah 1"},
      {"Key":"url 2","Value":"blah 2"},
      {"Key":"url 3","Value":"blah 3"}
     ]
}

SBJsonWriter produces the following for the matching object/type that I've created in objc:

{
  "TypeProperties": {
    "key 2": "object 2",
    "key 1": "object 1",
    "key 4": "object 4",
    "key 0": "object 0",
    "key 3": "object 3"
  },
  "ImageUrls": {
    "key 0": "url 0",
    "key 1": "url 1",
    "key 2": "url 2"
  }
}

This is how I'm using SBJsonWriter:

SBJsonWriter *writer = [[SBJsonWriter alloc] init];
writer.humanReadable = YES;
NSString* json = [writer stringWithObject:itemToAdd];

Here's my implementation of proxyForJson in the class being serialized (required by SBJsonWriter):

- (NSDictionary*) proxyForJson
{
      return [NSMutableDictionary dictionaryWithObjectsAndKeys:
                self.typeProperties, @"TypeProperties",
                self.imageUrls, @"ImageUrls",
                nil];
}

The class being serialized contains only the two properties: typeProperties and imageUrls (both are NSMutableDictionary).

Now, the problem: the server (not surprisingly) does not parse the Json produced by SBJsonWriter when I perform a POST. And the question: how do I produce Json that matches that which is provided by the server (assuming that matching Json would be parsed correctly on upload).

Thanks in advance for any help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here is a simple code demonstrating how to use SBJsonWriter:

#import <Foundation/Foundation.h>

#import "SBJsonWriter.h"


int main(int argc, const char * argv[])
{

    @autoreleasepool {

        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];

        // create JSON output from dictionary

        NSError *error = nil;
        NSString * jsonTest = [[[SBJsonWriter alloc] init] stringWithObject:jsonTestDictionary error:&error];

        if ( ! jsonTest ) {
            NSLog(@"Error: %@", error);
        }else{
            NSLog(@"%@", jsonTest);
        } 
    }
    return 0;
}

outputs

{
    "aDate":"2012-09-12 07:39:00 +0000",
    "aFloat":1.2345000505447388,
    "nestedObject":{
        "aStringInNestedObject":"nestedStringValue",
        "aNumberInNestedObject":1
     },
    "aJSonList":["arrayItem1","arrayItem2","arrayItem3"],
    "aString":"stringValue",
    "aNumber":1
}

Notes:

  1. Using 'error' allowed me to figure out that if you write [NSDate date] instead of [[NSDate date] description] you will get a "JSON serialisation not supported for __NSTaggedDate" error.
  2. notice the float rounding error... 1.2345 became 1.2345000505447388

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...