我正在使用REST api(来自HERE地图)进行各种服务。我试图解析JSON结果。一个有效,另一个没有。
第一个是方向服务,它会吐出两个位置之间的方向。这是JSON输出:
{"results":[{"waypoints":[{"id":"1 Infinite Loop","lat":37.331998,"lng":-122.03078,"sequence":0,"estimatedArrival":null,"estimatedDeparture":"2016-10-19T09:30:00+01:00","fulfilledConstraints":[]},{"id":"Cypress Drive","lat":37.330196,"lng":-122.021369,"sequence":1,"estimatedArrival":null,"estimatedDeparture":null,"fulfilledConstraints":[]}],"distance":"2589","time":"314","interconnections":[{"fromWaypoint":"1 Infinite Loop","toWaypoint":"Cypress Drive","distance":2589.0,"time":314.0,"rest":0.0,"waiting":0.0}],"description":"Targeted best time; with , improvement for traffic","timeBreakdown":{"driving":314,"service":0,"rest":0,"waiting":0}}],"errors":[],"processingTimeDesc":"139ms","responseCode":"200","warnings":null,"requestId":null}
下一个是具有更简单请求的不同API,它将对邮政地址进行地理编码。这是JSON输出:
{"results":{"MetaInfo":{"Timestamp":"2016-06-23T17:24:00.003+0000"},"View":[{"_type":"SearchResultsViewType","ViewId":0,"Result":[{"Relevance":0.89,"MatchLevel":"houseNumber","MatchQuality":{"State":1.0,"City":0.89,"Street":[0.87],"HouseNumber":1.0},"MatchType":"pointAddress","Location":{"LocationId":"NT_XJ6VcnP0-isqXdG1YBq.vA_xA","LocationType":"address","DisplayPosition":{"Latitude":37.33177,"Longitude":-122.03042},"NavigationPosition":[{"Latitude":37.33178,"Longitude":-122.03079}],"MapView":{"TopLeft":{"Latitude":37.3328942,"Longitude":-122.0318338},"BottomRight":{"Latitude":37.3306458,"Longitude":-122.0290062}},"Address":{"Label":"1 Infinite Loop, Cupertino, CA 95014, United States","Country":"USA","State":"CA","County":"Santa Clara","City":"Cupertino","Street":"Infinite Loop","HouseNumber":"1","PostalCode":"95014","AdditionalData":[{"value":"United States","key":"CountryName"},{"value":"California","key":"StateName"},{"value":"Santa Clara","key":"CountyName"},{"value":"N","key":"PostalCodeType"}]}}}]}]}}
我在swift中解析JSON的代码在第一个中运行得非常漂亮:
if let url = NSURL(string: therl),
JSONData = NSData(contentsOfURL: url),
json = try? NSJSONSerialization.JSONObjectWithData(JSONData, options: []),
dict = json as? [String: AnyObject],
results = dict["results"] as? [[String: AnyObject]] {
for result in results {
print (result)
}
} else {
print("unable to connect!")
}
但第二个失败!!
答案 0 :(得分:1)
在第二个结果 不一个Array
,它是Dictionary
已修改以显示如何恢复第一个纬度值
关注您的代码...
if let
url = NSURL(string: therl),
JSONData = NSData(contentsOfURL: url),
json = try? NSJSONSerialization.JSONObjectWithData(JSONData, options: []),
dict = json as? [String: AnyObject],
results = dict["results"] as? [String: AnyObject],
views = results["View"] as? [[String: AnyObject]]
{
for view in views
{
if let resultNodes = views["Result"] as? [[String: AnyObject]]
{
for resultNode in resultNodes
{
if let location = resultNode["Location"] as? [String: AnyObject]
{
if let address = location["Address"] as? [String: AnyObject]
{
// Process the address dictionary...
}
if let displayPosition = location["DisplayPosition"] as? [String: AnyObject]
{
// Process the Display Position dictionary...
let latitude: Double = displayPosition["Latitude"] as! Double
let longitude: Double = displayPosition["Longitude"] as! Double
}
// ... other values...
}
}
}
}
}