如何对来自Alamofire的JSON进行排序并返回最终的JSON对象(swiftyJSON) [英] How to sort JSON coming from Alamofire and return final JSON object (swiftyJSON)

查看:238
本文介绍了如何对来自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, "http://www.thesite.com/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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆