0

如何使用 JSONModel 库从以下 JSON 响应创建 ConvertOnDemand 'NSArray *':-

[
 {"id": 1, "name": "jim"},
 {"id": 2, "name": "lovy"}
]

如果您想了解更多 JSONModel ConvertOnDemand(https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=jsonmodel+convertonDemand),请查看此处。

4

3 回答 3

0

可能是BWJSONMatcher是您正在寻找的。

声明与您的 json 字符串匹配的数据模型:

@interface YourDataModel : NSObject
@property (nonatomic, assign) NSInteger id;
@property (nonatomic, strong) NSString *name;
@end

然后,您可以使用BWJSONMatcher将 json 字符串转换为 NSArray,该 NSArray 可以直接在您的 ViewControllers 中使用。

NSArray *jsonArray = [BWJSONMatcher matchJSON:jsonString withClass:[YourDataModel class]];
于 2015-11-10T03:37:04.850 回答
0

ConvertOnDemand 对于您的模型类是可选的,如果您的实体类是 Person 类,请像这样声明数组属性,

@property (nonatomic, strong) NSArray<Person, ConvertOnDemand> *persons;

@property (nonatomic, strong) NSArray<Person> *persons;

对于前一种来说,persons 数组类型是JSONModelArray[Person],它只是表示所有的 person 对象都符合 Person 协议。对于后一种,类型是NSArray未确认的对象,但它们实际上是 Person 类型。

于 2016-03-11T07:21:54.120 回答
0

如果您愿意改用本机Foundation框架,则可以执行以下操作

NSArray *arr = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&err];

如果您的 JSON 是字符串的形式,只需NSData在调用上述方法之前转换为

NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

该解决方案将为您NSArray提供NSDictionaries

于 2015-08-10T16:48:39.843 回答