SBJsonWriter嵌套的NSDictionary

Jim*_*y_m 2 json nsdictionary sbjson

我正在尝试使用Json序列化objc对象以将其发送到服务器.

同一服务器在GET上为此对象类型发送以下内容:

 {
   "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"}
     ]
}
Run Code Online (Sandbox Code Playgroud)

SBJsonWriter为我在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"
  }
}
Run Code Online (Sandbox Code Playgroud)

这就是我使用SBJsonWriter的方式:

SBJsonWriter *writer = [[SBJsonWriter alloc] init];
writer.humanReadable = YES;
NSString* json = [writer stringWithObject:itemToAdd];
Run Code Online (Sandbox Code Playgroud)

这是我在序列化类中的proxyForJson的实现(SBJsonWriter需要):

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

被序列化的类只包含两个属性:typeProperties和imageUrls(都是NSMutableDictionary).

现在,问题是:当我执行POST时,服务器(毫不奇怪)不会解析SBJsonWriter生成的Json.问题是:如何生成与服务器提供的Json匹配的Json(假设在上载时正确解析匹配的Json).

在此先感谢您的帮助.

Pas*_*cal 5

这是一个简单的代码,演示如何使用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;
}
Run Code Online (Sandbox Code Playgroud)

输出

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

笔记:

  1. 使用'error'允许我弄清楚如果你编写[NSDate date]而不是[[NSDate date] description],你将得到一个"__NSTaggedDate不支持JSON序列化"错误.
  2. 注意浮动舍入错误... 1.2345成为1.2345000505447388