[英]How to use sub dictionary in Mantle with Overcoat?
I decided to use Mantle to consume some JSON endpoints, but the structure of the JSON is different depending on whether you're GET
ing or POST
ing an object. 我决定使用Mantle来使用一些JSON端点,但是JSON的结构因要GET
对象还是对对象进行POST
而有所不同。 Take the users endpoint for instance: 以用户端点为例:
When requesting a user you get a response similar to this: 请求用户时,您会收到类似以下的响应:
{
"random_meta_data": "whatever",
"etc.": "etc.",
"payload": {
"username": "username",
"email": "username@email.com",
"etc.": "etc."
}
}
When creating a user you need to send something like this: 创建用户时,您需要发送以下内容:
{
"username": "username",
"email": "username@email.com",
"etc.": "etc."
}
It's not a difficult problem to solve, but it seems like a common enough problem that Mantle
should be able to solve it for you. 这不是一个很难解决的问题,但看起来很常见, Mantle
应该能够为您解决。
I know I could simply initialize the mantle model with dictionary[@"payload"]
, but Overcoat
is doing the mapping for me automatically and if I'm going to do that manually I'm not taking advantage of Overcoat
anymore. 我知道我可以简单地用dictionary[@"payload"]
初始化套模模型,但是Overcoat
会自动为我进行映射,如果我要手动进行映射,那么我就不会再利用Overcoat
了。
So I'm wondering if there is a standard way of solving this with Mantle
and/or Overcoat
? 所以我想知道是否有使用Mantle
和/或Overcoat
解决此问题的标准方法?
What you are describing is called an envelop response and you can read about how to deal with those in the Overcoat
README.md: 你所描述被称为一个信封响应,你可以了解如何处理那些在Overcoat
README.md:
https://github.com/Overcoat/Overcoat#envelop-and-error-responses https://github.com/Overcoat/Overcoat#envelop-and-error-responses
Other services like App.net use an envelop response, which is a top level JSON response containing the data requested and additional metadata. App.net等其他服务使用信封响应,这是包含所需数据和其他元数据的顶级JSON响应。 For these kind of services, you must create your own OVCResponse subclass and specify the data key path. 对于此类服务,必须创建自己的OVCResponse子类并指定数据键路径。
@interface AppDotNetResponse : OVCResponse
...
@end
@implementation AppDotNetResponse
+ (NSString *)resultKeyPathForJSONDictionary:(NSDictionary *)JSONDictionary
{
return @"data";
}
@end
You can then specify which response class to use in your client by overriding +responseClass. 然后,您可以通过覆盖+ responseClass来指定要在客户端中使用的响应类。
+ (Class)responseClass {
return [AppDotNetResponse class];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.