SwiftyJson无法设置值

时间:2016-03-04 05:20:42

标签: json swift sorting alamofire

我正在使用Alamofire来获得一张卡车桌。结果使用SwiftyJSON处理。其中一列是每辆卡车的坐标。

我想比较卡车与用户当前位置的坐标。我已经可以做计算了。我想把计算的距离加到同一个JSONObject中作为一个新的字段,意图按距离排序,但我似乎无法设置一个新的值。

设置json对象根本不起作用。以下是我的代码。我评论了它不起作用的部分。我没有收到警告或错误。如果我打印出来,我只是得到一个空行。请帮忙。提前致谢。我做得对吗?有关如何根据设备上的结果和用户位置中的卡车坐标给出最佳距离列表的建议吗?

 Alamofire.request(.POST, Ftf.endPoint(), parameters: params).responseJSON { response in

        if response.result.value != nil{
            let obj = JSON(response.result.value!)
            self.jsonObj = obj
            //ATTEMPT TO SORT THE JSON OBJECT
            print("Begin Sorting Object")
            print("User located at \(self.MyLocation)")

            for (key,subJson):(String, JSON) in self.jsonObj {
                let truckloc = subJson["truckLocation"].stringValue
                print("This truck is at \(truckloc)")

                let coords = truckloc
                var coord = coords.componentsSeparatedByString(",")
                let lat = coord[0]
                let lon = coord[1]
                let truckLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake((lat as NSString).doubleValue, (lon as NSString).doubleValue)
                let point1 = MKMapPointForCoordinate(self.MyLocation)
                let point2 = MKMapPointForCoordinate(truckLocation)
                let distance = MKMetersBetweenMapPoints(point1, point2)/1000
                let distanceStr = NSString(format: "%.1f", distance)

                print("Distance of truck from user is \(distanceStr)")

                //THIS LINE HERE DOESNT WORK. NO ERROR NO WARNINGS. JUST NOTHING HAPPENS
                self.jsonObj[key]["distance"].string = "\(distanceStr)"


            }

            self.activityIndicator.stopAnimating()
            self.tableView.reloadData()
            self.ftfRefresh.endRefreshing()
        }else{
            self.presentViewController(Ftf.generalAlert("No network connection found"), animated: true, completion: nil)
        }


    }

2 个答案:

答案 0 :(得分:4)

试试这个,

self.jsonObj [key] [“distance”] = JSON(“\(distanceStr)”)

示例示例

    var json = JSON(["1":"2"])

    print(json)

    json["1"] = JSON("3")

    print(json)

答案 1 :(得分:0)

我还没弄清楚原因,但我似乎解决了我的问题。 insted使用self.jsonObj,我使用了一个本地obj并添加了字段。完成后我更新了全局self.jsonObj。以下代码有效。密钥也作为字符串传递。我把它改成了Int

 Alamofire.request(.POST, Ftf.endPoint(), parameters: params).responseJSON { response in

        if response.result.value != nil{
            var obj = JSON(response.result.value!)
                            //ATTEMPT TO SORT THE JSON OBJECT
            print("Begin Sorting Object")
            print("User located at \(self.MyLocation)")

            for (key,subJson):(String, JSON) in obj {
                let truckloc = subJson["truckLocation"].stringValue
                print("This truck is at \(truckloc)")

                let coords = truckloc
                var coord = coords.componentsSeparatedByString(",")
                let lat = coord[0]
                let lon = coord[1]
                let truckLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake((lat as NSString).doubleValue, (lon as NSString).doubleValue)
                let point1 = MKMapPointForCoordinate(self.MyLocation)
                let point2 = MKMapPointForCoordinate(truckLocation)
                let distance = MKMetersBetweenMapPoints(point1, point2)/1000
                let distanceStr = NSString(format: "%.1f", distance)

                print("Distance of truck from user is \(distanceStr)")

                //FINALLY IT WORKED

                obj[Int(key)!]["dist"].stringValue = distanceStr as String
                print(obj[Int(key)!]["dist"].stringValue)


            }

            self.jsonObj = obj



            self.activityIndicator.stopAnimating()
            self.tableView.reloadData()
            self.ftfRefresh.endRefreshing()
        }else{
            self.presentViewController(Ftf.generalAlert("No network connection found"), animated: true, completion: nil)
        }




    }