如何使用RestKit映射JSON数组

时间:2022-10-24 15:07:53

I have json string like this format :

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

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

it does not have a key in the top level.

*没有钥匙。

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

mapping like this won't work , it give me the error:

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

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

How to map this type of json ?

如何映射这种类型的json?

2 个解决方案

#1


1  

Use

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

You should check "Mapping without KVC" section on Restkit Object Mapping Docs.

您应该在Restkit对象映射文档中选中“不使用KVC进行映射”部分。

Here is an example from the docs:

以下是文档中的示例:

[
    { "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"
    }
]

And you map that like this:

你就像这样映射:

// 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];

#2


0  

For me the solution was:

对我来说,解决方案是:

add this response descriptor, use the object inside de array

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

[manager addResponseDescriptor:[ObjectInsideTheArray getResponseDescriptor]];

and in the success response

并在成功的反应

NSArray *response = (NSArray *)[mappingResult array]; //instead of firstObject

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

#1


1  

Use

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

You should check "Mapping without KVC" section on Restkit Object Mapping Docs.

您应该在Restkit对象映射文档中选中“不使用KVC进行映射”部分。

Here is an example from the docs:

以下是文档中的示例:

[
    { "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"
    }
]

And you map that like this:

你就像这样映射:

// 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];

#2


0  

For me the solution was:

对我来说,解决方案是:

add this response descriptor, use the object inside de array

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

[manager addResponseDescriptor:[ObjectInsideTheArray getResponseDescriptor]];

and in the success response

并在成功的反应

NSArray *response = (NSArray *)[mappingResult array]; //instead of firstObject

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