我想序列化一個對象的NSArray。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,我看到了FO llowing:(這將匹配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吊艙,然後發出以下命令:
[Item testJsonSerialization];
[Item testJsonArraySerialization];
注意,也許我沒有正確配置映射爲數組序列。儘管這個映射器完美地用於上面的反序列化目標json文本。
檢查'RKObjectParameterization parametersWithObject:'的輸出,是你期望的嗎? – Wain
參數不是我所期望的,它們幾乎與json輸出相匹配。 – GreenKiwi
我會期望看到它是一個對象數組,其中每個對象是一個具有一組屬性的字典。 – GreenKiwi