如何从iOS中的文件解析JSON? [英] How do I parse JSON from a file in iOS?
问题描述
我试图解析来自JSON文件的数据。我试图把解析/获取的数据放入带有标签的UIView或webview中。
JSON文件如下所示:
{bodytext:< p> \\\
这里有一些关于某些网页(& lt;< em>网站< />)的线路。 ñ一些更多的东西在这里< / p>
}
显示如何解析从Web URL中检索到的JSON的溢出,但实际上我已经有了一个JSON文件,我想解析它,如何从文件中解析JSON?
-
创建空文本文件(新文件/其他/空)例如example.json
-
将json字符串粘贴到文件中。
-
使用这些行来获取数据:
NSString * filePath = [[NSBundle mainBundle] pathForResource:@exampleofType:@json];
NSData * data = [NSData dataWithContentsOfFile:filePath];
NSArray * json = [NSJSONSerialization JSONObjectWithDat a:数据选项:kNilOptions错误:无];
I am trying to parse data from a JSON file. I am trying to put that parsed/fetched data into a UIView with a label or in a webview. The JSON file looks something like the following:
{"bodytext": "<p>\n Some lines here about some webpage (“ <em>Site</>”) some more lines here. \n </p>\n\n <p>\n some more stuff here </p>
}
There are posts here on Stack Overflow showing how to parse JSON retrieved from a Web URL, but I actually already have a JSON file I want to parse. How do I parse JSON from a file?
Create empty text file (New File/Other/Empty) e.g. "example.json"
Paste json string into the file.
Use these lines to get the data:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"example" ofType:@"json"]; NSData *data = [NSData dataWithContentsOfFile:filePath]; NSArray *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
这篇关于如何从iOS中的文件解析JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!