//解析json字符串
+ (NSArray *)list{
NSMutableArray *tmpArrayM = [NSMutableArray array];
NSString *path = [[NSBundle mainBundle] pathForResource:@"products.json" ofType:nil];
// NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
// NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
NSData *data = [NSData dataWithContentsOfFile:path];
NSError *error;
NSArray *jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
if (jsonObject && error == nil) {
for (NSDictionary *dict in jsonObject) {
[tmpArrayM addObject:[self productModelWithDictionary:dict]];
}
return tmpArrayM;
} else {
return nil;
}
}
/*将字典转换成json/
- (NSString *) getJsonWithDictionary:(NSDictionary *)dict{
NSError *parseError = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&parseError];
NSString *jsonlocadata = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
return jsonlocadata;
}
作者:u011018979 发表于2017/7/3 15:16:55 原文链接
阅读:66 评论:0 查看评论