2017-05-02 1 views
-2

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.

+0

Regardez la structure JSON. C'est un tableau contenant un tableau de dictionnaires. – rmaddy

Répondre

0

Je n'utilise pas SwiftyJSON, parce que j'utilise JSONSerialization, donc je ne sais pas quel type de données votre variable json est, aussi je suppose que response.result.value est de type Data.

guard let json = try? JSONSerialization.jsonObject(with: response.result.value, options: .mutableContainers) else { return } 

guard let rootArray = json as? Array<Array<Dictionary<String,Any>>> else { return } 

let array = rootArray[0] // The outer/root array seems useless from what you have shown in your JSON above, so this is to get to the array of dictionaries. 

for item in array { 

    print(item["job_title"]) // prints out "Optional(job_title)" 
}