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

时间:2022-10-29 20:43:15

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:

我正在尝试从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>
}

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?

这里有一些关于栈溢出的文章,展示了如何解析从Web URL检索到的JSON,但实际上我已经有了一个要解析的JSON文件。如何从文件中解析JSON ?

3 个解决方案

#1


115  

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

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

  2. Paste json string into the file.

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

  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];
    

#2


18  

Swift 2.0 version of the accepted answer:

认可答案的Swift 2.0版本:

if let filePath = NSBundle.mainBundle().pathForResource("example", ofType: "json"), data = NSData(contentsOfFile: filePath) {
        do {
            let json = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments)
        }
        catch {
            //Handle error
        }
    }

#3


7  

I have followed this and it is working fine

我已经跟踪了这个,它运行得很好

NSError *error = nil;
 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"messages" 
                                                      ofType:@"json"];
 NSData *dataFromFile = [NSData dataWithContentsOfFile:filePath];
 NSDictionary *data = [NSJSONSerialization JSONObjectWithData:dataFromFile
                                                      options:kNilOptions
                                                        error:&error];
 if (error != nil) {
  NSLog(@"Error: was not able to load messages.");
  return nil;
 }

#1


115  

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

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

  2. Paste json string into the file.

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

  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];
    

#2


18  

Swift 2.0 version of the accepted answer:

认可答案的Swift 2.0版本:

if let filePath = NSBundle.mainBundle().pathForResource("example", ofType: "json"), data = NSData(contentsOfFile: filePath) {
        do {
            let json = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments)
        }
        catch {
            //Handle error
        }
    }

#3


7  

I have followed this and it is working fine

我已经跟踪了这个,它运行得很好

NSError *error = nil;
 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"messages" 
                                                      ofType:@"json"];
 NSData *dataFromFile = [NSData dataWithContentsOfFile:filePath];
 NSDictionary *data = [NSJSONSerialization JSONObjectWithData:dataFromFile
                                                      options:kNilOptions
                                                        error:&error];
 if (error != nil) {
  NSLog(@"Error: was not able to load messages.");
  return nil;
 }