How I Get JSON Data and add it to URL?
My Case is I Will access JSON URL, but i need value Token from other JSON URL.
This My Json, i need a token to access it.
https://api-sandbox.tiket.com/flight_api/all_airport/token=......
To get token, i must access from other JSON, this view the other JSON to get token.
{
"diagnostic":{"status":200,"elapsetime":"0.2082","memoryusage":"16.99MB","unix_timestamp":1499832598,"confirm":"success","lang":"id","currency":"IDR"},
"output_type":"json",
"login_status":"false",
"token":"b650fce732668b58b0a4c404b65932e972ad123d"
}
I must get value token, and add it to first JSON URL to access First JSON
This my coding :
func getLatestKotaTujuan() {
var tujuanUrl = URLComponents(string: "https://api-sandbox.tiket.com/flight_api/all_airport?")!
tujuanUrl.queryItems = [
URLQueryItem(name: "token", value: "4d4bba355bb920fbdc1aa3848769a59ba74ea03c" ),
URLQueryItem(name: "output", value: "json")
]
let request = tujuanUrl.url
let task = URLSession.shared.dataTask(with: request!, completionHandler: { (data, response, error) -> Void in
if let error = error {
print(error)
return
}
// Parse JSON data
if let data = data {
self.kotaTujuan = self.parseJsonData(data: data)
// Reload table view
OperationQueue.main.addOperation({
self.tableView.reloadData()
})
}
})
task.resume()
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…