gpt4 book ai didi

ios - JsonModelLib : Unable to parse if a variable does not exists in JSON

转载 作者:行者123 更新时间:2023-11-28 21:34:35 26 4
gpt4 key购买 nike

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

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 不包含 MODEL 中存在的变量 Why?? 因为我使用的 REST API 不会将变量渲染到 JSON 中(如果其中没有值) .

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

请帮忙或建议我该怎么做

注意:我已经在 JsonModelLib 方面走得很远了,所以如果您要求我使用其他东西或其他库,那么我会很痛苦。如果可能的话,请尝试在不更改库的情况下提出一些建议

非常感谢...

最佳答案

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

关于ios - JsonModelLib : Unable to parse if a variable does not exists in JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34390844/

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