【问题标题】:SBJsonWriter Nested NSDictionarySBJsonWriter 嵌套 NSDictionary
【发布时间】:2012-07-30 16:34:57
【问题描述】:

我正在尝试使用 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)。

提前感谢您的帮助。

【问题讨论】:

    标签: json nsdictionary sbjson


    【解决方案1】:

    这是一个简单的代码,演示如何使用 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 date] 而不是 [[NSDate date] description] 你会得到一个“JSON 不支持序列化 __NSTaggedDate”错误。
    2. 注意浮点舍入错误... 1.2345 变为 1.2345000505447388

    【讨论】:

    • 不错的一个.. 它真的是一个演示兼救生员:-)
    【解决方案2】:

    在 JSON 中,{ } 代表一个对象(键/值对),[ ] 代表一个数组。从您提供的示例来看,这是您的服务器所期望的:

    Top Object:具有两个键的字典: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];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多