J'ai une donnée sans nom JSON qui ressemble à ceci:Parse sans nom JSON Swift Dictionnaire
[
[
{
"job_title" : "job_title",
"fname" : "fname",
"department" : "department",
"email" : "",
"business_phone" : "business_phone",
"project" : null,
"location_id" : null,
"bio" : null,
"manager_id" : null,
"team_lead_id" : null,
"lname" : "lname",
"project_id" : null,
"title" : null,
"user_id" : 1,
"user_pass" : null,
"user_name" : "fname-lname",
"company" : "company",
"office_location_id" : "office_location"
},
{//Next employee record
//Data
}
]
]
Et essaie d'utiliser Alamofire et SwiftyJSON pour analyser les données dans un dictionnaire que je vais pouvoir utiliser dans d'autres parties de l'application.
Voici ce que j'ai essayé:
Alamofire.request(url, method: .post, encoding: JSONEncoding.default).validate().responseJSON{
response in
print("Request: \(String(describing: response.request))") // original URL request
print("Response: \(String(describing: response.response))") // HTTP URL response
print("Data: \(String(describing: response.data))") // server data
//print("Result: \(String(describing: response.result.value))") // result of response serialization
print("Error: \(String(describing: response.error))")
var json = JSON(response.result.value)
print(json)
//
// let fname: String = json[]["fname"].stringValue
// print(fname)
//
// for (index, object) in json {
// let fname = object["fname"].stringValue
// print(fname)
// }
// if let data = response.result.value?.data(using: String.Encoding.utf8){
// do {
// employeeDict = try JSONSerialization.jsonObject(with: data, options: []) as? [String: AnyObject]
//
// if let myDictionary = employeeDict{
// print("First name is: \(myDictionary["fname"]!)")
// }
// }
// }
// guard let object = response.result.value else{
// print("Error")
// return
// }
//
// let json = JSON(object)
// if let jsonArray = json.array{
// if let user_id = jsonArray[0]["user_id"].array{
// if let fname = jsonArray[1]["fname"].array{
// print(user_id)
// }
// }
// }
}
je peux imprimer les données JSON, mais ne peut accéder à aucune des informations ou stocker de celui-ci dans un dictionnaire.
Regardez la structure JSON. C'est un tableau contenant un tableau de dictionnaires. – rmaddy