Quantcast
Channel: CSDN博客移动开发推荐文章
Viewing all articles
Browse latest Browse all 5930

解析json字符串

$
0
0
//解析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 查看评论

Viewing all articles
Browse latest Browse all 5930

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>