簡體   English   中英

RestKit 中的 Object 映射

[英]Object Mapping in RestKit

我有這種格式的 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 進行 object 映射。 使用 RKManaged Object 的 map 的可能方法是什么。 有人可以幫忙嗎。 我被困了3天。 謝謝

Zach,您可能應該在RestKit郵件列表上提出這個問題,並更具體地說明您想要實現的目標。

無論如何,RestKit 作者 Blake Watters 剛剛發布了一個描述 Object 映射的新文檔,它應該可以回答您的所有問題。 祝你好運!

也許這沒有幫助,但我遇到了一個問題,即當我嘗試 map 我的對象時 RestKit 崩潰了,問題是我試圖 map 為 keypath 'description' 設置一個值。 這是不可行的,因為“描述”是 Objective-C 中的關鍵字。 嘗試將該名稱更改為其他名稱。

扎克,

無論 JSON 中對應映射的名稱是什么,都稱為keyPath 使用 RestKit 0.9.2,帶有新的 Object 映射:

我假設您需要從 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

問候,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM