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 进行对象映射。使用 RKManaged Object 映射此 JSON 的可能方法是什么。有人可以帮忙吗。我被困了3天。谢谢

4

3 回答 3

4

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

无论如何,RestKit 作者 Blake Watters 刚刚发布了一个描述 Object Mapping 的新文档,它应该可以回答您的所有问题。祝你好运!

于 2011-07-06T06:26:33.113 回答
1

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

于 2012-02-23T15:50:53.733 回答
0

扎克,

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

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

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

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

问候,

于 2011-07-14T06:24:45.957 回答