2012-08-01 62 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