1

在映射动态键时,我遇到了“找不到 keyPath 的对象映射:”的问题。

JSON响应是:

{
    5471 =     {
        ImageCount = 0;
        InventoryID = "3453dfdsfgt456t3";
        MakeName = aaa;
        ModelName = "a1";
        StockNumber = 5471;
        Year = 2012;
    };
    5516 =     {
        ImageCount = 0;
        InventoryID = "fg456tgev65464b4v";
        MakeName = bbb;
        ModelName = b1;
        StockNumber = 5516;
        Year = 2002;
    };
    5567 =     {
        ImageCount = 0;
        InventoryID = "fdg54646ghy67u65";
        MakeName = ccc;
        ModelName = c1;
        StockNumber = 5567;
        Year = 2008;
    };
}

上面的keyPath值5567,5516,5471是随机值,怎么定义key呢?我的课程代码如下:

班级:

@interface CategoryPic : NSObject
@property(nonatomic,strong)NSString *imageCount_pic;
@property(nonatomic,strong)NSString *inventoryID_pic;
@property(nonatomic,strong)NSString *makeName_pic;
@property(nonatomic,strong)NSString *modelName_pic;
@property(nonatomic,strong)NSString *stockNumber_pic;
@property(nonatomic,strong)NSString *year_pic;
@end

- (void)viewDidLoad{

RKObjectMapping* new = [RKObjectMapping mappingForClass:[CategoryPic class]];

    [new mapKeyOfNestedDictionaryToAttribute:@"stockid"];
    [new mapKeyPath:@"(stockid).ImageCount" toAttribute:@"imageCount_pic"];
    [new mapKeyPath:@"(stockid).InventoryID" toAttribute:@"inventoryID_pic"];
    [new mapKeyPath:@"(stockid).MakeName" toAttribute:@"makeName_pic"];
    [new mapKeyPath:@"(stockid).ModelName" toAttribute:@"modelName_pic"];
    [new mapKeyPath:@"(stockid).StockNumber" toAttribute:@"stockNumber_pic"];
    [new mapKeyPath:@"(stockid).Year" toAttribute:@"year_pic"];

[[NewSingelton class].objectManager.mappingProvider setMapping:new forKeyPath:@"stockid"];

}
4

2 回答 2

0

stockid应该作为属性存在,CategoryPic以便将值保存在某处。如果数字始终匹配StockNumber,那么您可以使用stockNumber_pic和删除该线映射。

于 2013-09-19T15:40:37.650 回答
0

我会将其转换为数组并将随机数用作字段之一。

NSMutableArray *result = [NSMutableArray array];
NSMutableDictionary *entry = [NSMutableDictionary dictionary];

for (NSString *key in jsonObject.allKeys) {
    entry = [NSMutableDictionary dictionaryWithDictionary:jsonObject[key]];
    [entry addEntriesFromDictionary:@{@"randomNumber" : key}];
    [result addObject:[NSDictionary dictionaryWithDictionary:entry];
}

或者,如果您的数据样本有任何迹象,请丢弃此密钥,因为它与 StockNumber 相同。

于 2013-09-19T10:18:37.703 回答