0

我知道我的问题有点令人困惑,所以请允许我详细说明。

JSON解析库:JSONModelLib

模型

#import "JsonModelLib/JSONModel.h"


@interface CustomerDetailsModel : JSONModel


@property (nonatomic, strong) NSString *description;

@property (nonatomic, strong) NSString *firstName;

@property (nonatomic, strong) NSString *lastName;

@property (nonatomic, strong) NSString *activatedTill;

@property (nonatomic, strong) NSString *birthdate;

@property (nonatomic, strong) NSString *phone;

@property (nonatomic, strong) NSString *id;

@property (nonatomic, strong) NSString *statusCode;

@end

JSON字符串

NSString* str = @"\
{\
\"description\": \"None\",\
\"firstName\": \"vikas\",\
\"lastName\": \"bansal\",\
\"activatedTill\": \"2016-01-17 09:04:11\",\
\"email\": \"bansal\",\
\"birthdate\": \"None\",\
\"phone\": \"None\",\
\"id\": \"1053\",\
\"statusCode\": \"1600\"\
}\
";

解析

CustomerDetailsModel* c = [[CustomerDetailsModel alloc] initWithString:str error:nil];

问题

问题是有时json不包含模型中存在的变量为什么?因为如果没有值,我正在使用的 REST API 不会将变量呈现为 JSON。

例如,假设birthdate 中没有值,那么它不会包含在 JSON 中,所以当我尝试解析 JSON 时,我会得到一个错误。

请帮助或建议我该怎么做

注意:我已经在J​​sonModelLib方面取得了很大进展,所以如果您要求我使用其他东西或其他一些库,那么我将非常痛苦。如果可能,请尝试在不更改库的情况下提出建议

非常感谢...

4

1 回答 1

2

使用可选的模型属性 - 在 repo 的 README 中有一个示例。 https://github.com/icanzilb/JSONModel#optional-properties-ie-can-be-missing-or-null

于 2015-12-21T07:58:16.980 回答