1

I would like to parse a local JSON file but I am unaware of how to do so in Swift 3

My Current Code doesn't seem to work

I keep receiving this error:

'jsonObject' produces 'Any', not the expected contextual result type 'NSArray'

import UIKit
import Alamofire
import SwiftyJSON

class ViewController: UIViewController
{

    var allEntries: String!


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func LoadAllQuestionsAndAnswers
    {
            let path = Bundle.main.path(forResource: "content", ofType: "json")
            let jsonData : NSData = NSData(contentsOfFile: path!)!
            allEntries = JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions.mutableContainers, error: nil) as String
            NSLog(allEntries)
    }

}

I am loading the data from this json file "content.json"

[
    {
        "id" : "1",
        "question": "What is the fastest fish in the ocean?",
             "answers": [
             "Sailfish",
             "Lion Fish",
             "Flounder",
             "Tiger Shark",
             "Swordfish"
            ],
         "difficulty": "1"
     },
     {
         "id" : "2",
         "question": "Which animal has the most legs?",
              "answers": [
              "Millipede",
              "Shrimp",
              "Octopus",
              "Dog",
              "Lion"
            ],
          "difficulty": "1"
     }
]
1
  • 1
    The code you have use doesn't even compile because from Swift 2 there is no parameter like error with this method instead you need to throw it using try catch. Commented Oct 20, 2016 at 4:38

2 Answers 2

3
let path =  Bundle.main.path(forResource: "sample", ofType: "son")
 let jsonData = try? NSData(contentsOfFile: path!, options: NSData.ReadingOptions.mappedIfSafe) 
print(jsonData!) 
let jsonResult: NSDictionary = try! JSONSerialization.jsonObject(with: jsonData as! Data, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary

// save below sample json in a file and name it as sample.json

{ "blogs": [ { "id": 111, "url": "http://roadfiresoftware.com/blog/", "name": "Roadfire Software Blog" }, { "id": 345, "url": "https://developer.apple.com/swift/blog/", "name": "Swift Developer Blog" } ] }

Sign up to request clarification or add additional context in comments.

Comments

2

The root object of the JSON is an array

var allEntries = [[String:Any]]()
...
allEntries = try! JSONSerialization.jsonObject(with: jsonData, options: []) as! [[String:Any]]

mutableContainers is not needed at all in Swift

1 Comment

Thanks for the response! I tried this and it was throwing me an arrow, I used [String:Any] instead of [[String:Any]] and I was able to get the return

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.