开发者

Object Mapping in RestKit

开发者 https://www.devze.com 2023-03-17 11:03 出处:网络
I have a JSON response of this format {\"status\": { \"id\":0, \"offerList\":[{\"offerId\":38,\"title\":\"TITLE OFFER 38\",\"shortDescription\":\"OFFER PCT 38\",\"orginalPrice\":100,\"image\":\"offer

I have a JSON response of this format

{"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开发者_StackOverflow":"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}]
}
}

I am using restkit for object mapping. What could be the possible way to map this JSON using RKManaged Object. Could someone help please. I am stuck for past 3 days. Thanks


Zach, you should probably ask this question on the RestKit mailing list and be a bit more specific as to what you want to achieve.

Regardless, RestKit author Blake Watters just posted a new document describing Object Mapping, which should answer all your questions. Good luck!


Maybe this is to no help, but I had a problem where RestKit crashed when trying to map my objects and the problem was that I tried to map a value to keypath 'description'. This was not doable though because 'description' is a keyword in Objective-C. Try change that name to something else.


Zach,

Whatever is the name of the corresponding mapping in JSON, you refer to as keyPath. Using RestKit 0.9.2, with new Object Mapping:

I'll assume that you need one-to-many relationship from Status (parent) to Offers and Shops (children), and the property names are kinda implicit here.

Usually in your app delegate, you can configure your mappings then like this:

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

Regards,

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号