RestKit中的对象映射

3
我有一个这样格式的JSON响应
{"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进行对象映射。使用RKManagedObject,可能有哪些方式来映射此JSON呢?请求有人帮助,我已经卡了3天了。谢谢。

3个回答

4

Zach,你应该在RestKit邮件列表上提出这个问题,并更加具体地说明你想要实现什么。

无论如何,RestKit的作者Blake Watters刚刚发布了一份新文档描述对象映射,它应该能回答你所有的问题。祝你好运!


嗨,这个链接似乎无法访问。你能否更新一下?谢谢。 - Zach
我已更新链接 - RestKit项目最近已移至github仓库,因此之前的链接无法使用。 - Victor K.
嗨,非常感谢。我想知道为什么我无法创建RKObjectMapping对象,而是得到了RKOBjectMapper。我需要导入其他类吗? - Zach
Blake Watters的新教程非常不错。非常感谢你,Blake! - Kurt

1
或许这并没有什么帮助,但我曾遇到一个问题:当尝试映射我的对象时,RestKit 崩溃了。问题是我试图将一个值映射到关键路径“description”。然而这是不可行的,因为“description”是 Objective-C 中的关键字。请尝试将该名称更改为其他内容。

0

Zach,

无论在JSON中对应的映射名称是什么,你都可以称之为keyPath。 使用RestKit 0.9.2和新的对象映射:

我假设你需要从状态(父级)到优惠和商店(子级)建立一对多关系,并且属性名称在这里有点隐含。

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

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!我需要导入其他头文件吗? - Zach
你使用的是哪个版本的RestKit?如果你使用的是标记为0.9.2的版本,并且包含了<RestKit/Restkit.h>,那么它应该有RKObjectMapping。 - cvbarros

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接