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 はランダムな値です。どのようにキーを定義できますか? 私のクラスコードは以下にあります:

クラス:

@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

これを配列に変換し、乱数をフィールドの 1 つとして使用します。

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 に答える