2016-12-07 28 views
-3

獲得價值我有Dictionary這樣,我得到「OutputResult」的價值,但我怎麼弄「的company_id」的價值,「部門」等。如何從字典swift3

if let convertedJsonIntoDict = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary 


let firstNameValue = convertedJsonIntoDict["OutputResult"] as? String 
       print("OutputResult is :\(firstNameValue!)") 

{ 
OutputResult = true; 
msg =  { 
    "company_id" = 1; 
    "department_id" = 6; 
    email = "[email protected]"; 
    "employee_code" = 014; 
    "employee_id" = 131; 
    "employee_name" = "abc dsc"; 
    "have_power" = 0; 
    ip = ""; 
    "is_deleted" = 0; 
    lastactivity = ""; 
    status = 1; 
}; 
} 

回答

1

您需要使用快速原生Dictionary而不是NSDictionary,它會讓您從Dictionary輕鬆訪問。從您的迴應看起來像OutputResult包含Boolean值,也是你的company_iddepartment_id是在另一個字典msg所以你嘗試下面的東西。

if let convertedJsonIntoDict = try JSONSerialization.jsonObject(with: data!, options: []) as? [String: Any] { 
    if let msg = convertedJsonIntoDict["msg"] as? [String: Any], let company_id = msg["company_id"] as? Int, 
     let department_id = msg["department_id"] as? Int { 
      //You can same way access other details of employee, email, etc.. 
      print(company_id) 
      print(department_id) 
    } 
} 
+0

感謝您rply但錯誤在這行 **如果讓味精= convertedJsonIntoDict [ 「味精」],讓COMPANY_ID =味精[ 「COMPANY_ID」]作爲? Int, let department_id = msg [「department_id」] as? Int ** 其顯示類型「任何」沒有下標成員 – johnyDiOS

+0

@johny忘記了添加'as? [字符串:任何]'檢查編輯的答案。 –

+0

其工作我得到它,並感謝您的教學 – johnyDiOS