开发者社区> 问答> 正文

ios Mantle中解析Json,无法忽略无用key,同时不能自定义key,遇到过嘛?

user.h
#import <Mantle/Mantle.h>

@interface user : MTLModel <MTLJSONSerializing>

@property (nonatomic, strong) NSNumber *userId;
@property (nonatomic, strong) NSNumber *createdTime;
@property (nonatomic, strong) NSNumber *startIndex;
@property (nonatomic, strong) NSNumber *endIndex;
@property (nonatomic, strong) NSNumber *stype;
@property (nonatomic, assign, readonly) BOOL isDeleted;

@property (nonatomic, strong) NSString *phone;
@property (nonatomic, strong) NSString *password;


@end

user.m
#import "user.h"


@implementation user

+ (NSDictionary *)JSONKeyPathsByPropertyKey
{
    return @{@"userId":@"id"};

}

- (instancetype)initWithDictionary:(NSDictionary *)dictionaryValue error:(NSError *__autoreleasing *)error
{
    self = [super initWithDictionary:dictionaryValue error:error];
    if (self == nil) return nil;

    // Store a value that needs to be determined locally upon initialization.
    //     _retrievedAt = [NSDate date];

    return self;
}


@end

调用
DHUser *user = [[DHUser alloc]initWithDictionary:[response objectForKey:@"user"] error:&error];

执行以后
产出的效果有以下问题
1.userId没能够成功对应id
2.我没有建立headPic,不需要,但是强制要求。

screenshot

展开
收起
a123456678 2016-07-27 16:22:17 2926 0
1 条回答
写回答
取消 提交回答
  • 解决了,粗心了,初始化DHUser user = [[DHUser alloc]initWithDictionary:[response objectForKey:@"user"] error:&error]; 修改为DHUser user = [MTLJSONAdapter modelOfClass:DHUser.class fromJSONDictionary:[response objectForKey:@"user"] error:&error];

    使用MTLJSONAdapter进行声明。

    2019-07-17 20:00:48
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
手淘iOS性能优化探索 立即下载
From Java/Android to Swift iOS 立即下载
深入剖析iOS性能优化 立即下载

相关镜像