如何使用NSJSONSerialization类参考读取JSON文件? [英] How to read a JSON file using the NSJSONSerialization Class Reference?

查看:101
本文介绍了如何使用NSJSONSerialization类参考读取JSON文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用NSJSONSerialization类参考读取JSON文件,我发现的有关此类使用的所有示例都从网页本身读取内容,而不是从先前创建的JSON文件中读取.Anyone知道如何使用该类从JSON文件解析?谢谢。

I need to read a JSON file using the NSJSONSerialization Class Reference, and all the examples that I have found about the use of this class read the content from the webpage itself, instead of reading from a JSON file that has been previously created.Anyone knows how to parse from a JSON file using that class? Thanks.

推荐答案

简单,快速的例子:

NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"foobar"
                                                     ofType:@"json"];
NSData *data = [NSData dataWithContentsOfFile:jsonPath];
NSError *error = nil;
id json = [NSJSONSerialization JSONObjectWithData:data
                                          options:kNilOptions
                                            error:&error];
NSLog(@"JSON: %@", json);

这篇关于如何使用NSJSONSerialization类参考读取JSON文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆