如何对来自Alamofire的JSON进行排序并返回最终的JSON对象(swiftyJSON)

编程入门 行业动态 更新时间:2024-10-06 10:34:08
本文介绍了如何对来自Alamofire的JSON进行排序并返回最终的JSON对象(swiftyJSON)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我无法简洁地从api中提取数据,将用户当前位置添加到对象中,然后根据计算出的距离对数据进行排序。

stackoverflow问题并不能完全回答我面临的问题。请参阅此处:

并测试了我的答案在带有SwiftyJSON的操场上:

如果愿意,还可以直接对SwiftyJSON对象进行排序:

let sortedResults =结果.sort {$ 0.0.1 [ distance]。doubleValue< $ 0.1.1 [ distance]。doubleValue} .map {$ 0.1}

但我发现少了可读为源代码。

I'm having trouble succinctly pulling data from an api, adding the users current location into the object and then sorting the data based on the calculated distance.

The stackoverflow questions don't quite answer the problem I'm facing. See here: How to sort posts read from JSON server file in Swift.

I'm currently loading api data from Alamofire and rendering that data with a UITableViewController.

override func viewDidLoad() { super.viewDidLoad() titleLabel.title = q.capitalizedString Alamofire.request(.GET, "www.thesite/api/v1/things", parameters: ["q" : q]) .responseJSON { response in let JSONObject = JSON(response.result.value!) self.results = JSONObject["things"] for (key, _) in self.results { let intKey: Int = Int(key)! var thisItem: JSON = self.results[intKey] let geoLat = thisItem["place"][0]["location"]["geo"][1].double ?? 37.763299 let geoLong = thisItem["place"][0]["location"]["geo"][0].double ?? -122.419356 let destination = CLLocation(latitude: geoLat, longitude: geoLong) let setLocation = CLLocation(latitude: self.currentLocation.latitude, longitude: self.currentLocation.longitude) let distanceBetween: CLLocationDistance = destination.distanceFromLocation(setLocation) thisItem["distance"].double = distanceBetween self.results[intKey] = thisItem } self.tableView.reloadData() } }

I get the data from the api and successfully add the distance between the user's location and the destination of the place.

However, now I need to sort the JSON object (SwiftyJSON) from lowest to highest distance. This is where I'm stuck.

the data structure at the point the tableView (as JSON object) is reloaded is essentially:

results = [ {"title": "Chai", "distance": "1245.678575"}, {"title": "Espresso", "distance": "765845.678575"}, {"title": "Drip Coffee", "distance": "23445.678575"} ... ]

How would I be able to either: 1) convert the object to NSArray and sort; or 2) just sort the object? When would be the best place to do the distance add and sort - should I do it before converting to the JSON object.

Any help is appreciated! Thanks!

解决方案

If results is a SwiftyJSON object, you can extract its array with .arrayValue.

let resultsArray = results.arrayValue

Then once you have a normal array of dictionaries, you can then sort the array with sort like this:

let sortedResults = resultsArray.sort { $0["distance"].doubleValue < $1["distance"].doubleValue }

I took your JSON snippet:

And tested my answer in a Playground with SwiftyJSON:

If you wanted to, you could also sort the SwiftyJSON object directly:

let sortedResults = results.sort { $0.0.1["distance"].doubleValue < $0.1.1["distance"].doubleValue }.map { $0.1 }

But I find it less readable as source code.

更多推荐

如何对来自Alamofire的JSON进行排序并返回最终的JSON对象(swiftyJSON)

本文发布于:2023-11-26 21:46:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635318.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   Alamofire   JSON   swiftyJSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!