我正在尝试序列化对象的 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
项目.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
执行 Have RestKit pod installed 然后发出以下命令:
[Item testJsonSerialization];
[Item testJsonArraySerialization];
请注意,也许我没有为数组序列化正确配置映射。虽然这个映射器非常适合反序列化上面的目标 json 文本。