2012-08-01 61 views
2

我想序列化一个objc对象与Json发送它到服务器。SBJsonWriter嵌套的NSDictionary

即同一服务器发送以下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]; 

这是我的代理实施ForJson在类被序列化(由SBJsonWriter必需):

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

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

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

在此先感谢您的帮助。

回答

2

在JSON中,{ }代表一个对象(键/值对),而[ ]代表一个数组。您所提供的样品来看,这里就是你的服务器预计:

顶部对象:字典,两个键:TypePropertiesImageUrls

TypePropertiesImageUrls:各自为含有一个或多个对象具有两个键的数组: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]; 
} 
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
+0

不错的..它真的是一个DEMO暨生命的救星:-) – 2013-02-13 05:20:08