html - 如何从 iOS 中的文件中解析 JSON?

标签 html ios json parsing

我正在尝试解析 JSON 文件中的数据。我正在尝试将解析/获取的数据放入带有标签的 UIView 或 webview 中。 JSON 文件如下所示:

{"bodytext": "<p>\n Some lines here about some webpage (&ldquo; <em>Site</>&rdquo;) some more lines here. \n </p>\n\n <p>\n some more stuff here </p>
}

这里有关于 Stack Overflow 的帖子,展示了如何解析从 Web URL 检索到的 JSON,但实际上我已经有了一个想要解析的 JSON 文件。如何从文件中解析 JSON?

最佳答案

  1. 创建空文本文件(新文件/其他/空),例如“example.json”

  2. 将json字符串粘贴到文件中。

  3. 使用这些行获取数据:

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"example" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    NSArray *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    

关于html - 如何从 iOS 中的文件中解析 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18520949/

相关文章:

c# - 在输入 json 中将值 {null} 转换为类型 'System.DateTime' 时出错

android - 如何使用对象键作为 id 序列化 JSON?

html - 使用 flexbox 垂直居中

html - 为什么要将图像大小放在 HTML 代码中

iOS 关闭 iPad 屏幕

ios - MPMoviePlayerViewController 以不寻常的外观锁定应用程序?

html - 使用 Asp.net 和 CSS 调整图像大小

javascript - 移动端剑道网格刷新问题

javascript - Hybrid App - 使用 iPhone 数字键盘,我可以添加小数点吗?

php - cUrl 正在返回 JSON 字符串,json_decode 没有将其解码为对象或数组