1

也许这里有一个类似的问题,但我找不到。所以我有 RestKit 0.2pre4 并想映射这样的 JSON ...

{
    "location": {
        "position": {
            "latitude": 53.9675028,
            "longitude": 10.1795594
        }
    },
    "id": "da3224f2-5919-42f2-9dd8-9171088f4ad7",
    "name": "Something even more beautiful",
}

...对于这样一个对象:

@interface FavoritePlace : NSManagedObject

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * placeId;
@property (assign, nonatomic) double latitude;
@property (assign, nonatomic) double longitude;

@end

如您所见,关键是我不想创建关系并将“位置”对象单独存储在数据库中 - 这没有任何意义。因此,我想将那些嵌套的东西(location.position.latitude 和 location.position.longitude)分配给对象本身的特定属性。如何做到这一点?

4

2 回答 2

1

Apparently, I did a wild ass guess and it worked! Here's the mapping which does the trick:

[placeMapping addAttributeMappingsFromDictionary:@{
     @"id": @"placeId",
     @"name": @"name",
     @"location.position.latitude": @"latitude",
     @"location.position.longitude": @"longitude"     
     }];

Have not found this in documentation, but maybe it's just me... Anyway, if something logical, but undocumented, works, then framework should be good. :) RestKit FTW!

于 2012-12-20T19:07:42.983 回答
0

您可以将包含 json 字符串的字符串传递给 nsdata

NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

然后,您将能够使用 iOS API 解析 de json 数据

NSError *error;
NSDictionary *json = jsonData ? [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error] : nil;

现在,检查生成的 NSDictionary(例如使用 NSlog),填充您的“FavoritePlace”对象并将其持久化。

希望有所帮助!

于 2012-12-20T18:34:33.343 回答