gpt4 book ai didi

ios - 地幔 : Uncaught exception 'NSInvalidArgumentException' when parsing JSON

转载 作者:行者123 更新时间:2023-11-28 18:56:09 25 4
gpt4 key购买 nike

我正在使用 Mantle 框架,我似乎在将某些值序列化为 MTLModel 时遇到了一些问题。这是我从服务器收到的 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"
}
}
}
}

我已经按照以下方式设置了我的 MTLModel:

用户

@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

简介

@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

图片

@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

版本

@interface Versions : MTLModel <MTLJSONSerializing>

@property (nonatomic) NSString *thumb;
@property (nonatomic) NSString *small;

@end

@implementation Versions

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{
@"thumb": @"thumb",
@"small": @"small"
};
}

@end

我正在执行以下 Overcoat POST 调用以获取 JSON。当我 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);
}
}];

当我尝试像这样抓取 Picture 对象时出现问题:

Picture *picture = [[user profile] picture];

我得到一个异常(exception):

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary picture]: unrecognized selector sent to instance 0x7f95026d4210'

我该如何解决这个问题?我做错了什么?

最佳答案

您没有正确解析您的 json。图片字典嵌套在配置文件中。得到它喜欢的东西

Nsmutabledictinary *profile=[jsondic objectforkey:@"Profile"];

Nsmutabledictinary *picture=[profile objectforkey:@"Picture"];

请更正语法错误,因为我没有在编辑器中编写代码,因此代码中可能存在语法错误。谢谢

关于ios - 地幔 : Uncaught exception 'NSInvalidArgumentException' when parsing JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32345137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com