SBJsonWriter嵌套的NSDictionary(SBJsonWriter Nested NS

2019-06-26 16:48发布

我试图序列化JSON的一个objc对象将其发送到服务器。

同一服务器发送下列关于获取该对象类型:

 {
   "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)。

现在,问题:服务器(不奇怪)不分析由SBJsonWriter产生时我执行POST的JSON。 而这样的问题:我如何产生相匹配的是由服务器提供JSON(假设匹配的Json将上载正确解析)。

在此先感谢您的帮助。

Answer 1:

在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];
}


Answer 2:

下面是一个简单的代码演示如何使用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的日期]描述]你会得到错误“不支持__NSTaggedDate JSON序列化”。
  2. 注意到浮动舍入误差... 1.2345成为1.2345000505447388


文章来源: SBJsonWriter Nested NSDictionary