[英]Mantle: Uncaught exception 'NSInvalidArgumentException' when parsing JSON
I am using the Mantle
framework, and I seem to be having some problems with serializing some values into a MTLModel
. 我正在使用Mantle
框架,并且在将某些值序列化为MTLModel
似乎遇到了一些问题。 This is the JSON
I am receiving from the server: 这是我从服务器接收到的JSON
:
{
"id":50,
"name":"UserName",
"email":"user@username.com",
"profile":{
"picture": {
"original": "http://original.com/picture",
"versions": {
"thumb": "http://thumb.com/picture",
"small": "http://small.com/picture"
}
}
}
}
I've setup my MTLModel
s in the following manner: 我已经通过以下方式设置了MTLModel
:
User 用户
@interface User : MTLModel <MTLJSONSerializing>
@property (nonatomic, readonly) NSNumber *id;
@property (nonatomic) NSString *name;
@property (nonatomic) NSString *email;
@property (nonatomic) Profile *profile;
@end
@implementation User
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{
@"id": @"id",
@"name": @"name",
@"email": @"email",
@"profile": @"profile"
};
}
- (NSValueTransformer *)profileJSONTransformer {
return [MTLValueTransformer reversibleTransformerWithForwardBlock:^(NSDictionary *profileDict) {
return [MTLJSONAdapter modelOfClass:Profile.class
fromJSONDictionary:profileDict
error:nil];
} reverseBlock:^(Profile *profile) {
return [MTLJSONAdapter JSONDictionaryFromModel:profile];
}];
}
@end
Profile 轮廓
@interface Profile : MTLModel <MTLJSONSerializing>
@property (nonatomic) Picture *picture;
@end
@implementation Profile
+ (NSDictionary *)JSONKeyPathsByPropertyKeys {
return @{
@"picture": @"picture"
};
}
- (NSValueTransformer *)pictureJSONTransformer {
return [MTLValueTransformer reversibleTransformerWithForwardBlock:^(NSDictionary *picDict) {
return [MTLJSONAdapter modelOfClass:Picture.class
fromJSONDictionary:picDict
error:nil];
} reverseBlock:^(Picture *picture) {
return [MTLJSONAdapter JSONDictionaryFromModel:picture];
}];
}
@end
Picture 图片
@interface Picture : MTLModel <MTLJSONSerializing>
@property (nonatomic) NSString *original;
@property (nonatomic) Versions *versions;
@end
@implementation Picture
+ (NSDictionary *)JSONKeyPathsByPropertyKeys {
return @{
@"original": @"original",
@"versions": @"versions"
};
}
- (NSValueTransformer *)versionsJSONTransformer {
return [MTLValueTransformer reversibleTransformerWithForwardBlock:^(NSDictionary *versionDict) {
return [MTLJSONAdapter modelOfClass:Versions.class
fromJSONDictionary:versionDict
error:nil];
} reverseBlock:^(Versions *versions) {
return [MTLJSONAdapter JSONDictionaryFromModel:versions];
}];
}
@end
Versions 版本
@interface Versions : MTLModel <MTLJSONSerializing>
@property (nonatomic) NSString *thumb;
@property (nonatomic) NSString *small;
@end
@implementation Versions
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{
@"thumb": @"thumb",
@"small": @"small"
};
}
@end
I am doing the following Overcoat
POST call to grab the JSON. 我正在执行以下Overcoat
POST调用以获取JSON。 I am receiving the JSON fine when I NSLog
the response: 我在NSLog
响应中收到JSON罚款:
[[Client getInstance] POST:@"authorize.json" parameters:params completion:^(id response, NSError *error) {
if (!error) {
User *user = [MTLJSONAdapter modelOfClass:[User class] fromJSONDictionary:[response result] error:nil];
Picture *picture = [[user profile] picture];
NSLog(@"%@", picture);
} else {
NSLog(@"%@", error);
}
}];
The problem occurs when I try to grab the Picture
object like so: 当我尝试像这样抓取Picture
对象时,会发生问题:
Picture *picture = [[user profile] picture];
I get an exception of: 我有一个例外:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary picture]: unrecognized selector sent to instance 0x7f95026d4210'
How can I fix this? 我怎样才能解决这个问题? What am I doing wrong? 我究竟做错了什么?
you are not parsing your json properly.the picture dictionary is nested inside profile. 您没有正确解析json。图片字典嵌套在配置文件中。 get it like something 像东西一样得到它
Nsmutabledictinary *profile=[jsondic objectforkey:@"Profile"]; Nsmutabledictinary * profile = [jsondic objectforkey:@“ Profile”];
Nsmutabledictinary *picture=[profile objectforkey:@"Picture"]; Nsmutabledictinary *图片= [个人资料key:@“图片”];
please correct the syntax errors as i didnt write the code in editor so there can be syntax error in the code .thanks 请更正语法错误,因为我没有在编辑器中编写代码,因此代码中可能存在语法错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.