RestKit - 将嵌套的json映射到模型类

时间:2012-11-23 15:36:49

标签: iphone objective-c restkit geojson

我有json,我想要mapp到模型类

{
 "type": "FeatureCollection",
 "features": [
 {
   "geometry": {
     "coordinates": [100.0, 0.0, 129, 1356044399]
   },
   "properties": {
    "name": "My first name",
    "description": "Popular place"
   }
 },
 {
   "geometry": {
     "coordinates": [100.0, 1.0, 127, 1356045399]
   },
   "properties": {
    "name": "My second TIP",
    "description": "Even more popular place"
   }
 }
 ]
}

我想把它放到一个类似这样的模型类中:

@property longitude (taken from coordinates index 0)
@property latitude (taken from coordinates index 1)
@property name
@property description

1 个答案:

答案 0 :(得分:2)

嗯,我想出的解决方案如下:

两种型号:

@interface WTFeaturesModel
@property (nonatomic, strong) NSString *type;
@property (nonatomic, strong) NSArray *features;

@interface WTFeatureModel
@property (nonatomic, strong) NSDictionary *properties;
@property (nonatomic, strong) NSDictionary *geometry;

模型映射:

[featureMapping addAttributeMappingsFromArray:@[@"properties", @"geometry"]];

[featuresMapping addAttributeMappingsFromArray:@[ @"type"]];
[featuresMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"features" toKeyPath:@"features" withMapping:featureMapping]];

在我创建了这些模型后,我必须将数据映射到具有正确属性的其他模型。无论如何,这使得映射工作......