Bonjour à tous,
Je trouve beaucoup d'exemples sur le parsing de fichier JSON mais aucun pour parser un fichier composé uniquement d'array.
Je ne trouve pas de solution qui fonctionne et commence à me casser la tête ...
Ainsi, je recherche un peu d'éclairage en la matière
Mon fichier JSON est composé comme suite :
[[1,"Vélo"],[2,"Voiture"],[3,"Avion"],[4,"Camion"]]
Voici mon code source :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| if let path = NSBundle.mainBundle().URLForResource(jsonFile, withExtension: "json") {
let data = NSData(contentsOfURL: path)
let datastring = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("json: \(datastring)")
do {
let object = try NSJSONSerialization.JSONObjectWithData(data!, options: [])
if let dictionary = object as? [Dictionary<Int, AnyObject>] {
for item in dictionary {
let id = item[0] as! Int
let name = item[1] as! String
print("\(id) \(name)")
}
} else {
print("dictionary null")
}
} catch {
print("Error while reading the file: \(jsonFile).json")
}
} else {
print("Cannot find the file: \(jsonFile).json")
} |
Et voici l'affichage produit à la console :
1 2
| json: Optional([[1,"Vélo"],[2,"Voiture"],[3,"Avion"],[4,"Camion"]])
dictionary null |
Quoi qu'il en soit, merci d'avance.
Partager