使用奇数JSON的RestKit对象映射

时间:2012-10-24 03:54:43

标签: objective-c ios json restkit

您好我正在尝试对象映射以下JSON:

{    
"0":{       
       "chapter":"four",       
       "book":"Time Machine"    
    },    
"1":{       
      "chapter":"nine",       
      "book":"Science"    
    },    
"2":{       
      "chapter":"one",       
      "book":"Fiction Man"    
    },    
"3":{       
      "chapter":"25",       
      "book":"Cars"    
    } 
}

我正在使用的代码:

RKObjectMapping *bookMapping = [RKObjectMapping mappingForClass:[Book class]];

[[RKObjectManager sharedManager].mappingProvider addObjectMapping:bookMapping];

[retailerMapping mapKeyPathsToAttributes:@"chapter", @"chapter",@"book", @"book", nil];
[objectManager.mappingProvider setMapping:bookMapping forKeyPath:@"0"];

这适用于第一组,但显然会跳过1,2和3.我怎样才能使这个工作?

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

如果您可以控制JSON的结构,则将其更改为数组以代替字典。否则,您可以尝试将mapKeyPath映射到key.attribute,例如0.chapter

JSON示例:

{chapters:[    
   {       
   "chapter":"four",       
   "book":"Time Machine"    
   },    
  {       
  "chapter":"nine",       
  "book":"Science"    
  },    
  {       
  "chapter":"one",       
  "book":"Fiction Man"    
  },    
  {       
  "chapter":"25",       
  "book":"Cars"    
  }
 ] 
}