关于 iphone:RestKit 中的对象映射 | 珊瑚贝

Object Mapping in RestKit


我有一个这种格式的 JSON 响应

1
2
3
4
5
6
7
8
{“status”:
{
“id”:0,
“offerList”:[{“offerId”:38,“title”:“TITLE OFFER 38”,“shortDescription”:“OFFER PCT 38”,“orginalPrice”:100,“image”:“offer-38.jpg”,“description”:“DESCRIPTION OFFER 38”,“shopId”:4,“startDate”:“5/29/2011 12:00:00 AM”,“endDate”:“8/10/2011 12:00:00 AM”,“startTime”:“16:30:00”,“endTime”:“21:59:59”,“insertionDate”:“7/5/2011 4:42:40 AM”,“categoryId”:0,“subCategoryId”:0,“published”:1}

“shopList”:[{“id”:4,“name”:“Store 1”,“street”:“Street Store 1”,“postCode”:“88214”,“city”:“Barcelona”,“state”:“”,“country”:“Spain, Kingdom of”,“description”:“Loc Description Store 1”,“url”:“http://www.store-1.com”,“email”:“store1@gmail.com”,“telephone”:“Phone-number-store-1”,“published”:1,“lastUpdated”:“7/5/2011 4:42:40 AM”,“latitude”:41.4398,“longitude”:2.1601}]
}
}

我正在使用 restkit 进行对象映射。使用 RKManaged Object 映射此 JSON 的可能方法是什么。
有人可以帮忙吗。我被困了3天。
谢谢


Zach,您可能应该在 RestKit 邮件列表中提出这个问题,并更具体地说明您想要实现的目标。

不管怎样,RestKit 作者 Blake Watters 刚刚发布了一个描述对象映射的新文档,它应该可以回答您的所有问题。祝你好运!

  • 你好,这个链接好像失效了。能否请您更新。谢谢
  • 我已更新链接 – RestKit 项目最近已移至 github 存储库,因此链接无效
  • 嗨,非常感谢。我想知道为什么我无法创建 RKObjectMapping 对象。相反,我得到了 RKOBjectMapper。我需要导入其他一些类吗
  • Blake Watters 的新教程非常好。非常感谢布莱克!


也许这无济于事,但我遇到了一个问题,即在尝试映射我的对象时 RestKit 崩溃了,问题是我试图将一个值映射到 keypath ‘description’。这是不可行的,因为 ‘description’ 是 Objective-C 中的一个关键字。尝试将该名称更改为其他名称。


扎克,

JSON 中对应映射的名称是什么,都称为 keyPath。
使用带有新对象映射的 RestKit 0.9.2:

我假设您需要从 Status(父)到 Offers 和 Shops(子)的一对多关系,并且属性名称在这里有点隐含。

通常在您的应用委托中,您可以像这样配置映射:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
RKObjectMapping* statusMapping = [RKObjectMapping mappingForClass:[Status class]];
[statusMapping mapKeyPathToAttributes:@“statusId”,@“id”] // This maps JSON status.id to objc Status.statusId

RKObjectMapping* offerMapping = [RKObjectMapping mappingForClass:[Offer class]];
// Add more pairs as needed in the line below
[offerMapping mapKeyPathToAttributes:@“offerId”,@“offerId”,@“title”,@“title”,@“shortDescription”,@“shortDescription”];

//Do the same thing for your Shop mapping

//Configure relationships:
//Assumes Status class has a property of type NSArray* offers that will contain multiple orders
[statusMapping mapKeyPath:@“orderList” toRelationship:@“orders” withObjectMapping:offerMapping];

//Do the same thing for your Shop relationship

问候,

  • 嗨,我找不到 RKObjectMapping,而是得到了 RKObjectMapper!我需要导入任何其他标题吗?
  • 你使用的是什么版本的 RestKit?我敢肯定,如果你有 0.9.2 标记版本,如果你包含 <RestKit/Restkit.h> 它有 RKObjectMapping


来源:https://www.codenong.com/6576795/

微信公众号
手机浏览(小程序)
0
分享到:
没有账号? 忘记密码?