如何从iOS中的文件解析JSON? [英] How do I parse JSON from a file in iOS?

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

问题描述

我试图解析来自JSON文件的数据。我试图把解析/获取的数据放入带有标签的UIView或webview中。
JSON文件如下所示:

  {bodytext:< p> \\\
这里有一些关于某些网页(& lt;< em>网站< />)的线路。 ñ一些更多的东西在这里< / p>
}

显示如何解析从Web URL中检索到的JSON的溢出,但实际上我已经有了一个JSON文件,我想解析它,如何从文件中解析JSON?

解决方案


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

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

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

      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 (&ldquo; <em>Site</>&rdquo;) 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?

解决方案

  1. Create empty text file (New File/Other/Empty) e.g. "example.json"

  2. Paste json string into the file.

  3. 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屋!

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