【问题标题】:Object Mapping in RestKitRestKit 中的对象映射
【发布时间】:2011-07-04 23:19:37
【问题描述】:

我有一个这种格式的 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天。 谢谢

【问题讨论】:

    标签: iphone objective-c ios restkit


    【解决方案1】:

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

    不管怎样,RestKit 作者 Blake Watters 刚刚发布了一个 new document describing Object Mapping,它应该可以回答你所有的问题。祝你好运!

    【讨论】:

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

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

    【讨论】:

      【解决方案3】:

      扎克,

      无论 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
      

      问候,

      【讨论】:

      • 您好,我找不到 RKObjectMapping,而是得到了 RKObjectMapper!我需要导入任何额外的标题吗?
      • 你使用的是什么版本的 RestKit?我敢肯定,如果你有 0.9.2 标记版本,如果你包含 它有 RKObjectMapping
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-12
      • 1970-01-01
      • 2017-12-15
      相关资源
      最近更新 更多