首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >地幔:解析JSON时未见异常的“NSInvalidArgumentException”

地幔:解析JSON时未见异常的“NSInvalidArgumentException”
EN

Stack Overflow用户
提问于 2015-09-02 05:19:37
回答 1查看 247关注 0票数 0

我正在使用Mantle框架,在将某些值序列化为MTLModel时,我似乎遇到了一些问题。这是我从服务器接收到的JSON

代码语言:javascript
复制
{
    "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

用户

代码语言:javascript
复制
@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

代码语言:javascript
复制
@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

图片

代码语言:javascript
复制
@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

版本

代码语言:javascript
复制
@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罚款:

代码语言:javascript
复制
[[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对象时,会出现这样的问题:

代码语言:javascript
复制
Picture *picture = [[user profile] picture];

我有一个例外:

代码语言:javascript
复制
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary picture]: unrecognized selector sent to instance 0x7f95026d4210'

我怎么才能解决这个问题?我做错了什么?

EN

回答 1

Stack Overflow用户

发布于 2015-09-02 05:30:48

您没有解析您的json properly.the图片字典嵌套在概要文件中。把它弄成某种东西

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

Nsmutabledictinary *picture=profile objectforkey:@“图片”;

请更正语法错误,因为我没有在编辑器中编写代码,所以代码.thanks中可能有语法错误

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32345137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档