我一直在我的应用中集成UBER ride API。我是swift的初学者,我还没有完全理解从URI解析JSON / Dictionary等的概念。当我预订优步时,我得到了这样的回复
{"status":"processing","product_id":"28d5215d","destination":{"latitude":10.0216,"longitude":76.27394},"driver":null,"pickup":{"latitude":10.01319,"longitude":76.27691},"request_id":"5aa6fe30-9eed-4f19-8a8f-c0ed8aa1bfae","location":null,"vehicle":null,"shared":false}
然后当一个驱动程序接受我的请求时,我从同一个dataRequestWithURL得到这样的响应。我这样做是通过在响应中调用相同的dataTask直到驱动程序接受它为止。
{"status":"accepted","product_id":"6fd04ea","destination":{"latitude":10.0216,"eta":8,"longitude":76.27394},"driver":{"phone_number":"+919562117444","rating":4.62,"picture_url":"https:\/\/d1w2poirtb3as9.cloudfront.net\/token.jpeg","name":"FULL NAME","sms_number":null},"pickup":{"latitude":10.01319,"eta":5,"longitude":76.27691},"request_id":"5aa6fe30-9eed-4f19-8a8f-c0ed8aa1bfae","location":{"latitude":10.0062228,"bearing":249,"longitude":76.2780944},"vehicle":{"make":"Maruti Suzuki","picture_url":null,"model":"Ritz","license_plate":"LICENSE#"},"shared":false}
如何根据条件解析此问题。
这是我现在的代码。
if let dict = (try? NSJSONSerialization.JSONObjectWithData(data!, options: [])) as? [String: AnyObject],
let status = dict["status"] as? String,
let driver = fareDict["driver"] as? [String:AnyObject] {
do
{
//assign the value and update UI
} catch let error {
print("OK")
}
}
答案 0 :(得分:1)
如果我理解正确,你只想在接受乘车时解析字典?
如果是这样,您可以执行以下操作:
//MARK : Check if rider has accepted the ride
if let dict = (try? NSJSONSerialization.JSONObjectWithData(data!, options: [])) as? [String: AnyObject],
let status = dict["status"] as? String{
if status == "accepted"
{
print("Accepted")
}
else
{
self.getUberRideLive()
}
}