可能是RKEntityMapping JSON数组RKResponseDescriptor keypath问题

时间:2013-10-22 22:32:55

标签: json core-data restkit restkit-0.20

我正在尝试将我的核心数据“组件”实体映射到此响应JSON中的“热点”键路径,但它只会到达数组对象,因此它不会映射。我无法判断我的问题是映射还是响应描述符或两者的组合。

"result" : {
"type" : "Slidedeck",
"id" : 81,
"name" : "Brendantest",
"slides" : [
  {
    "thumb" : "http:\/\/api.idetailapp.review.thingee.com\/v4\/resources\/1294\/download\/slide?thumb=true",
    "id" : 1294,
    "notes" : "",
    "label" : "",
    "order" : 1,
    "parent_id" : 81,
    "file" : "slide-20.jpg",
    "components" : {
      "hotspots" : [
        {
          "x" : 205,
          "options" : "embedded",
          "type" : "video",
          "id" : 6082,
          "y" : 453,
          "assets" : [
            {
              "thumb" : "\/system\/asset_objects\/14\/original_thumb\/14.png",
              "id" : 14,
              "parent_id" : 6082,
              "file" : "slide_15_chart_animation_ipad_r03.mov",
              "url" : "http:\/\/api.idetailapp.review.thingee.com\/v4\/resources\/14\/download\/asset"
            }
          ],
          "parent_id" : 1294,
          "width" : 320,
          "height" : 246
        }
      ]
    },
    "url" : "http:\/\/api.idetailapp.review.thingee.com\/v4\/resources\/1294\/download\/slide"
  }
],
"version" : "0.43"

} }

这是我的RKEntityMapping:

RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Component"
                                               inManagedObjectStore:[RKManagedObjectStore  defaultStore]];


mapping.identificationAttributes = @[ @"componentID" ];


[mapping addAttributeMappingsFromDictionary:@{@"id": @"componentID",
                                              @"parent_id": @"parentID"
                                              }];
[mapping addAttributeMappingsFromArray:@[@"x", @"y", @"width", @"height", @"type", @"options"]];

这是我的Responsedescriptor:

componentResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:componentEntityMapping 
method:RKRequestMethodAny pathPattern:nil  
keyPath:@"result.slides.components.hotspots"         statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

我输入了RestKit / ObjectMapping调试跟踪并获得以下输出:

Asked to map source object (
    {
    assets =         (
                    {
            file = "slide_15_chart_animation_ipad_r03.mov";
            id = 14;
            "parent_id" = 6082;
            thumb = "/system/asset_objects/14/original_thumb/14.png";
            url = "http://api.idetailapp.review.thingee.com/v4/resources/14/download/asset";
        }
    );
    height = 246;
    id = 6082;
    options = embedded;
    "parent_id" = 1294;
    type = video;
    width = 320;
    x = 205;
    y = 453;
}

进一步调试消息:

将keyPath'id'的值转换为'NSNumber'类型的表示失败:Error Domain = org.restkit.RKValueTransformers.ErrorDomain Code = 3002“值转换失败”(     6082 )UserInfo = 0x12479350 {NSLocalizedDescription =预期inputValue类型NSNull,但得到__NSArrayI。}“ ),

1 个答案:

答案 0 :(得分:1)

您的问题是keyPath:@"result.slides.components.hotspots"

具体而言,slides是一个数组。

结果是要求RestKit将一组对象映射到您的映射中。因此,当它试图获取id(应该是一个数字)时,它会得到一个数组(因此你的错误)。这是因为在数组上调用valueForKey:会返回一个数组...

所以,基本上,你不能按照你想要的方式去做,因为JSON中间的数组。

如果您可以更改JSON,请执行此操作。如果不能,则需要更改映射以首先映射slides然后映射嵌套组件(如果需要,可以随后丢弃幻灯片)。