RestKit 0.20 NSArray序列化问题

时间:2013-04-17 19:28:30

标签: json restkit restkit-0.20

我正在尝试序列化NSArray对象。

该对象序列化为:

{"age":1,"name":"Foo"}

如果我有一个包含这些对象的NSArray,它应序列化为:

[{"age":1,"name":"Bob"},{"age":4,"name":"Sally"},{"age":2,"name":"Jill"}]

但是,当我通过RestKit直接序列化时,我得到以下内容:

{"age":[1,3,2],"name":["Sally","Jack","Bob"]}

当我检查RKMIMETypeSerialization时,我看到以下内容:(这将匹配json输出)

Parameters: {
    age =     (
        1,
        3,
        2
    );
    name =     (
        Sally,
        Jack,
        Bob
    );
}

我确信我只是在做一些非常愚蠢的事情,我一直在玩它无法搞清楚。

这是我的映射逻辑

+ (RKObjectMapping *)mapping {
    RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Item class]];
    [mapping addAttributeMappingsFromDictionary:@{
            @"name" : @"name",
            @"age" : @"age"
    }];

    return mapping;
}

这是执行数组序列化的逻辑:

+ (NSString *)JSONStringFromArray:(NSArray *)array {
    RKObjectMapping *mapping = [Item mapping];
    RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:mapping.inverseMapping objectClass:[Item class] rootKeyPath:nil];
    NSError *error;
    NSDictionary *parameters = [RKObjectParameterization parametersWithObject:array requestDescriptor:requestDescriptor error:&error];

// Serialize the object to JSON
    NSData *JSON = [RKMIMETypeSerialization dataFromObject:parameters MIMEType:RKMIMETypeJSON error:&error];

    NSString *jsonString = [[NSString alloc] initWithBytes:[JSON bytes]
                                                    length:[JSON length] encoding:NSUTF8StringEncoding];
    return jsonString;
}

以下是测试类: 的 Item.h

#import <Foundation/Foundation.h>

@interface Item : NSObject
@property(nonatomic, strong) NSString *name;
@property(nonatomic) int age;

+ (void)testJsonSerialization;

+ (void)testJsonArraySerialization;

+ (Item *)itemWithName:(NSString *)string age:(int)age;
@end

Item.m

#import "Item.h"
#import <RestKit.h>

@implementation Item
+ (RKObjectMapping *)mapping {
    RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Item class]];
    [mapping addAttributeMappingsFromDictionary:@{
            @"name" : @"name",
            @"age" : @"age"
    }];

    return mapping;
}

- (NSString *)JSONString {
    RKObjectMapping *mapping = [Item mapping];
    RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:mapping.inverseMapping objectClass:[Item class] rootKeyPath:nil];
    NSError *error;
    NSDictionary *parameters = [RKObjectParameterization parametersWithObject:self requestDescriptor:requestDescriptor error:&error];

// Serialize the object to JSON
    NSData *JSON = [RKMIMETypeSerialization dataFromObject:parameters MIMEType:RKMIMETypeJSON error:&error];

    NSString *jsonString = [[NSString alloc] initWithBytes:[JSON bytes]
                                                    length:[JSON length] encoding:NSUTF8StringEncoding];
    return jsonString;
}

+ (NSString *)JSONStringFromArray:(NSArray *)array {
    RKObjectMapping *mapping = [Item mapping];
    RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:mapping.inverseMapping objectClass:[Item class] rootKeyPath:nil];
    NSError *error;
    NSDictionary *parameters = [RKObjectParameterization parametersWithObject:array requestDescriptor:requestDescriptor error:&error];

// Serialize the object to JSON
    NSData *JSON = [RKMIMETypeSerialization dataFromObject:parameters MIMEType:RKMIMETypeJSON error:&error];

    NSString *jsonString = [[NSString alloc] initWithBytes:[JSON bytes]
                                                    length:[JSON length] encoding:NSUTF8StringEncoding];
    return jsonString;
}

+ (void)testJsonSerialization {
    Item *item = [Item itemWithName:@"Foo" age:1];
    NSString *itemJSON = [item JSONString];
    NSLog(@"ItemJSON:\n%@\n\n", itemJSON);
}

+ (void)testJsonArraySerialization {
    NSArray *items = @[[Item itemWithName:@"Sally" age:1], [Item itemWithName:@"Jack" age:3], [Item itemWithName:@"Bob" age:2]];
    NSString *itemJSON = [Item JSONStringFromArray:items];
    NSLog(@"ItemArrayJSON:\n%@\n\n", itemJSON);
}

+ (Item *)itemWithName:(NSString *)name age:(int)age {
    Item *item = [[Item alloc] init];
    item.name = name;
    item.age = age;
    return item;
}
@end

执行 安装RestKit pod,然后发出以下命令:

    [Item testJsonSerialization];
    [Item testJsonArraySerialization];

注意,也许我没有为数组序列化正确配置映射。虽然这个映射器非常适合反序列化上面的目标json文本。

1 个答案:

答案 0 :(得分:1)

预计

RKObjectParameterization将用于序列化对象映射目标的各个实例,并将实例静态序列化为NSMutableDictionary。您无法编写与RKObjectParameterization一起使用的映射,该映射将对象数组序列化为字典数组。您需要通过循环遍历数组来自己完成此操作。