开发者社区> 问答> 正文

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 2923 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
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
神龙云服务器产品及技术深度解析 立即下载
弹性创造价值:基于ECS的最佳性价比实践解析 立即下载
又快又稳:阿里云下一代虚拟交换机解析 立即下载

相关镜像