如何从以下JSON响应中使用JSONModel库创建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),请选中此处。
发布于 2015-08-11 00:48:40
如果您愿意改用原生Foundation框架,您可以这样做
NSArray *arr = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&err];如果您的JSON是字符串形式的,只需在调用上述代码之前转换为NSData即可
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];此解决方案将为您提供NSDictionaries的NSArray
发布于 2015-11-10 11:37:05
也许BWJSONMatcher就是你要找的。
声明与json字符串匹配的数据模型:
@interface YourDataModel : NSObject
@property (nonatomic, assign) NSInteger id;
@property (nonatomic, strong) NSString *name;
@end然后,您可以使用BWJSONMatcher将json字符串转换为可直接在ViewControllers中使用的NSArray。
NSArray *jsonArray = [BWJSONMatcher matchJSON:jsonString withClass:[YourDataModel class]];发布于 2016-03-11 15:21:54
ConvertOnDemand是您的模型类的可选,如果您的实体类是Person类,请像这样声明数组属性。
@property (nonatomic, strong) NSArray<Person, ConvertOnDemand> *persons;
@property (nonatomic, strong) NSArray<Person> *persons;
对于前者,person数组类型是JSONModelArray[Person],它只是表示所有的Person对象都符合Person协议。对于后一种类型,类型是带有未确认对象的NSArray,但它们实际上是Person类型。
https://stackoverflow.com/questions/31922829
复制相似问题