我希望使用restkit映射以下JSON。
Application
对象
"ObjectDetails":
[
{
"ObjectName":"Test1",
"Properties":
{
"PropertyName1":1,
"PropertyName3":2
}
},
{
"ObjectName":"Test2",
"Properties":
{
"PropertyName1":1,
"PropertyName2":"asdf"
}
},
{
"ObjectName":"Test3",
"Properties":
{
"PropertyName1":1,
"PropertyName6":3423,
"PropertyName7":"test",
"PropertyName3":2
}
}
]
映射
@interface ObjectDetail : NSObject
@property (nonatomic, strong) NSString *objectName;
//????
@end
我的问题是'属性'具有动态属性。我还没有找到将属性映射到任何东西的方法。理想情况下,将Properties映射到键/值字典会很不错。我确实需要字段名称和值。
即
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[ObjectDetail class]];
[mapping addAttributeMappingsFromDictionary:@{@"ObjectName":@"objectName"}];
RKResponseDescriptor *rd = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodAny pathPattern:nil keyPath:@"ObjectDetails" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
有没有人知道这方面的解决方案或知道如何实现它?
更新:基于@Idali解决方案,非常简单。如果您发现这有用,请务必提供答案!
对象
@interface ObjectDetail : NSObject
@property (nonatomic, strong) NSString *objectName;
@property (nonatomic, strong) NSMutableDictionary *properties;
@end
映射
@interface ObjectDetail : NSObject
@property (nonatomic, strong) NSString *objectName;
@property (nonatomic, strong) id *properties;
@end
然后投下它。
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[ObjectDetail class]];
[mapping addAttributeMappingsFromDictionary:@{@"ObjectName":@"objectName", @"Properties": @"properties"}];
答案 0 :(得分:1)
您可以通过设置"属性"来解决它。在coredata中输入transformable
属性,并在ObjectCalss中将其声明为id
并映射它。
如果您创建ManagedObject子类,它将为:
@property (nonatomic, retain) id properties;
阅读您可以投射的内容: .... ....
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
ObjectDetail* objectDetail =[fetchedObjects firstObject];
self.myDictionnay = (NSDcitionnary*) objectDetail.properties;
NSArray* keys = [self.myDictionnay allKeys]; //to check for existence of any key