user3371261 user3371261 - 1 year ago 98
JSON Question

JSON from file Swift 3.0 do not work

I have a problem with JSON, I want to read file JsonData.json using that code(I traied many ways), and always get that error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSJSONSerialization dataWithJSONObject:options:error:]: Invalid top-level type in JSON write'

Someone can help me?

func readPropertyList(){
if let path = Bundle.main.path(forResource:"JsonData", ofType: "json"){
//if let url = Bundle.main.url(forResource:"JsonData", withExtension: "json") {
let url: URL = URL(fileURLWithPath: path)
do {
let data: Data = try! Data(contentsOf: url , options: .alwaysMapped)
//let swiftDictionary = try PropertyListSerialization.propertyList(from: data, options: [], format: nil) as! [String]
//list = swiftDictionary
let thisJSON = try! data, options: [] )
} catch {


Maybe my, JSON file is wrong but I try verify on and that page say is fine.


"Sea food":"Owoce morza",
"Crayfish":"Rak słodkowodny",
"Dairy products":"Produkty mleczne",
"Cottage cheese":"Twar\u00f3g",


Answer Source

You're using the wrong API. It's the other way round

let thisJSON = try! JSONSerialization.jsonObject(with: data, options: [] )
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download