5

我有这种格式的json字符串:

[{"image":"/0001.jpg","link":"/index.php"},
{"image":"/0001.jpg","link":"/index.php"}]

它在顶层没有键。

[mapping mapKeyPath:@"image" toAttribute:@"image"];

像这样的映射不起作用,它给了我错误:

restkit.object_mapping:RKObjectMapper.m:81 Adding mapping error: Could not find an object mapping for keyPath: ''

如何映射这种类型的 json ?

4

2 回答 2

1

利用

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

您应该检查Restkit Object Mapping Docs 上的“ Mapping without KVC ”部分。

这是文档中的一个示例:

[
    { "title": "RestKit Object Mapping Intro",
      "body": "This article details how to use RestKit object mapping...",
      "author": {
          "name": "Blake Watters",
          "email": "blake@restkit.org"
      },
      "publication_date": "7/4/2011"
    }
]

你像这样映射:

// Our familiar articlesMapping from earlier
RKObjectMapping* articleMapping = [RKObjectMapping mappingForClass:[Article class]];
[articleMapping mapKeyPath:@"title" toAttribute:@"title"];
[articleMapping mapKeyPath:@"body" toAttribute:@"body"];
[articleMapping mapKeyPath:@"author" toAttribute:@"author"];
[articleMapping mapKeyPath:@"publication_date" toAttribute:@"publicationDate"];

[[RKObjectManager sharedManager].mappingProvider addObjectMapping:articleMapping];
于 2012-06-10T20:55:11.887 回答
0

对我来说,解决方案是:

添加此响应描述符,使用 de 数组中的对象

[manager addResponseDescriptor:[ObjectInsideTheArray getResponseDescriptor]];

并在成功响应中

NSArray *response = (NSArray *)[mappingResult array ]; //而不是firstObject

于 2015-01-30T10:49:18.440 回答