Cocoa-Touch - How parse local Json file

前端 未结 5 1292
鱼传尺愫
鱼传尺愫 2020-12-14 03:40

I\'m newbie in iOS dev and I\'m trying to parse a local Json file such as

{\"quizz\":[{\"id\":\"1\",\"Q1\":\"When Mickey was born\",\"R1\":\"1920\",\"R2\":\"1

5条回答
  •  离开以前
    2020-12-14 04:26

    Swift 2.3 I use a utility method to convert JSON files to a Dictionary:

    func getDictionaryFromJSON(jsonFileName: String) -> [String: AnyObject]? {
        guard let filepath = NSBundle.mainBundle().pathForResource(jsonFileName, ofType: "json") else {
            return nil
        }
    
        guard let data = NSData(contentsOfFile: filepath) else {
            return nil
        }
    
        do {
            let dict = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String: AnyObject]
            return dict
        } catch {
            print(error)
            return nil
        }
    }
    

提交回复
热议问题