SBJsonWriter嵌套的NSDictionary

时间:2012-08-01 18:08:38

标签: 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"}
     ]
}

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"
  }
}

这就是我使用SBJsonWriter的方式:

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

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

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

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

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

提前感谢您的帮助。

2 个答案:

答案 0 :(得分: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;
}

输出

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

注意:

  1. 使用'错误'让我弄清楚如果你写[NSDate 日期]而不是[[NSDate日期]描述]你会得到一个“JSON 不支持序列化 __NSTaggedDate“错误。
  2. 注意浮动舍入错误... 1.2345变为1.2345000505447388

答案 1 :(得分:2)

在JSON中,{ }表示对象(键/值对),[ ]表示数组。根据您提供的示例判断,这是您的服务器所期望的:

热门对象:包含两个键的词典:TypePropertiesImageUrls

TypeProperties ImageUrls :每个数组都包含一个或多个包含两个键的对象:KeyValue。每个密钥都应该有各自的值。

为了符合服务器的期望,你需要一个与此类似的结构(注意这只是一个简单的例子,直接写在这里,但应该指向正确的方向):

NSDictionary *object = [NSDictionary dictionaryWithObjectsAndKeys:
                        @"prop 0", @"Key",
                        @"blah 0", @"Value",
                        nil];

NSArray *typeProperties = [NSArray arrayWithObjects:
                           object, // Add as many similar objects as you want
                           nil];

NSArray *imageUrls = [NSArray arrayWithObjects:
                      object, // Add as many similar objects as you want
                      nil];

然后,在proxyForJson方法中,您可以使用:

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