-2
我有一個看起來像這樣的無名JSON數據:解析無名JSON斯威夫特詞典
[
[
{
"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
}
]
]
,我試圖用Alamofire和SwiftyJSON解析數據到一個字典,我就可以使用在應用程序的其他部分。
這裏是我已經試過:
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)
// }
// }
// }
}
我可以打印出來的JSON數據,但不能訪問的任何信息或存儲任何的它在字典中。
看看JSON結構。它是一個包含字典數組的數組。 – rmaddy