我如何解析来自 URI 的 2 个响应中的字典
How do I parse a Dictionary which come in 2 response from a URI
我已经在我的应用程序中集成了 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")
}
}
如果我没理解错的话,你只是想在接受乘车时解析字典?
如果是这样,您可以执行以下操作:
//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()
}
}
我已经在我的应用程序中集成了 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")
}
}
如果我没理解错的话,你只是想在接受乘车时解析字典?
如果是这样,您可以执行以下操作:
//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()
}
}