【发布时间】:2014-02-01 05:53:21
【问题描述】:
我一直在我的应用程序中使用 JSON
在我的 Android 应用程序中,我使用 Gson 反序列化来自服务器的 JSON 对象。
我喜欢 Gson 的一点是,我需要做的就是创建一个具有给定属性的 POJO 类,然后它们会自动映射到 Gson.fromJSON(JsonString, POJO.class);
如果我希望对象的名称不同于它们从服务器到达的名称,我只需添加一个注释 @SerializedName("server'sName")`
RestKit 有没有这样的方法?
手动命名所有属性似乎很乏味?
如果我添加/删除/重命名属性怎么办?
目前这是我的课
@interface Test
@property (nonatomic,assign) int prop1;
@property (nonatomic, assign) NSString *prop2;
@end
这是我的 JSON:
{ "prop1":10, "prop2":"test"}
这是我的映射
//map class component
RKObjectMapping *statsMapping = [RKObjectMapping mappingForClass:[Test class]];
[statsMapping addAttributeMappingsFromDictionary:@{@"prop1":@"prop1",@"prop2":@"prop2"}];
// Register our mappings with the provider using a response descriptor
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping
method:RKRequestMethodGET
pathPattern:nil
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:responseDescriptor];
我希望RestKit 映射到我的属性(如果它们同时存在于JSON 和我的类中)而不用名称命名它们
【问题讨论】:
标签: ios json restkit restkit-0.20