Wednesday, 21 February 2018

Get the Json Values from Text File - Swift

Get the Json Values from Text File - Swift

func get_json_from_txt_file(fileName: String) -> NSDictionary {
    if let path = Bundle.main.path(forResource: fileName, ofType: "txt") {
        do {
            let str_file_contents = try String(contentsOfFile: path, encoding: .utf8)
            let jsonData = str_file_contents.data(using: .utf8)!
            let json = try JSONSerialization.jsonObject(with: jsonData, options: []) as! NSDictionary
            return json
        }
        catch {
            print(error)
        }
    }
    let dict = NSDictionary()
    return dict
}

No comments: