RestKit:映射JSON字符串数组 [英] RestKit: mapping JSON array of strings

查看:207
本文介绍了RestKit:映射JSON字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定以下JSON:

  {
someKey:someValue,
otherKey:otherValue,
features:[
feature1,
feature2,
feature3
]
} b $ b

我将此JSON映射到 NSManagedObject RKMapperOperation RKEntityMapping (在本例中,我将有两个实体映射:一个用于顶级对象,另一个用于我的Feature类)。



顶级对象映射是微不足道的:两个属性映射以及与Feature的关系的一个(特征)关系。



我的问题是,如何将特征JSON数组映射到一个Feature对象数组? Feature类只有一个属性 name ,其中我想存储feature1,feature2等等,以及对父对象(顶级对象)的引用。类似这样:

  @interface特性:NSManagedObject 

//在实现文件中使用@dynamic声明。
@property(nonatomic,retain)NSString * name;
@property(nonatomic,retain)MyTopLevelObject * myTopLevelObject;任何想法?




@end


解决方案

您需要使用nil键路径:

  RKEntityMapping * featureMapping = [RKEntityMapping mappingForEntityForName:...]; 
[featureMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@name]];
featureMapping.identificationAttributes = @ [@name];然后,在顶级对象映射上,定义关系:



  [topMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@featurestoKeyPath:@featureswithMapping:featureMapping]]; 

在您的功能(在模型中), myTopLevelObject 应定义为与顶级对象的双向关系。


Given the following JSON:

{
   "someKey":"someValue",
   "otherKey":"otherValue",
   "features":[
      "feature1",
      "feature2",
      "feature3"
   ]
}

I am mapping this JSON into NSManagedObjects with RKMapperOperation and RKEntityMapping(in this example I would have 2 entity mappings: one for the top level object and another one for my Feature class).

The top level object mapping is trivial: two attribute mappings plus a relationship one(features) for the relation with Feature.

My question is,how to map the features JSON array into an array of Feature objects? The Feature class has just one property name where I want to store "feature1", "feature2", etc plus a reference to the parent object (the top level one). Something like this:

@interface Feature : NSManagedObject

//In the implementation file both properties are declared with @dynamic.
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) MyTopLevelObject *myTopLevelObject;

@end

Any idea?

解决方案

You need to use a nil key path:

RKEntityMapping *featureMapping = [RKEntityMapping mappingForEntityForName:...];
[featureMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"name"]];
featureMapping.identificationAttributes = @[ @"name" ];

Then, on your top level object mapping, define the relationship:

[topMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"features" toKeyPath:@"features" withMapping:featureMapping]];

In your feature (in the model), myTopLevelObject should be defined as a bi-directional relationship to the top level object.

这篇关于RestKit:映射JSON字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆