从AF ResponseJSON获取数据 [英] get data from AF responseJSON

查看:375
本文介绍了从AF ResponseJSON获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有处理位置数据的代码,因此我可以提取可以匿名化数据的详细信息-例如,如果我的places API表示它是_type:建筑物,而建筑物: Safeway,则可以保存数据作为经/纬度的 md5:安全路,并且在检查我的位置数据时所有安全路看起来都一样。

I have code to process location data so I can extract details that can anonymize the data -- for example, if my places API says it is a _type: "Building" and building: "Safeway" -- I could save the data as an md5 of the lat/long:"safeway", and all safeways would look the same when inspecting my location data. That's also what I want.

static func getLocationData(location: CLLocation, _ callback: @escaping (CLLocation?) -> Void) {
    let parameters = [
            "q": location.coordinate.latitude.description + "," + location.coordinate.longitude.description,
            "key": Places.OPENCAGEDATA_API_KEY
        ]

    AF.request(Places.uri, method: .get, parameters: parameters, encoding: URLEncoding(destination: .queryString)).responseJSON { response in
        switch response.result {

        case .success(let json):
            print(json)
            DispatchQueue.main.async {

               callback(location)

            }
        case .failure(let error):
            print(error)

            callback(nil)
        }
    }
}

此交易有效,如我所见:

This transaction works, as I see printed:

{
    documentation = "https://opencagedata.com/api";
    licenses =     (
                {
            name = "CC-BY-SA";
            url = "https://creativecommons.org/licenses/by-sa/3.0/";
        },
                {
            name = ODbL;
            url = "https://opendatacommons.org/licenses/odbl/summary/";
        }
    );
    rate =     {
        limit = 2500;
        remaining = 2496;
        reset = 1556150400;
    };
    results =     (
                {
            annotations =             {
                DMS =                 {
                    lat = "MYLAT N";
                    lng = "MYLONG W";
                };
                FIPS = ...

但是现在json只是Any类型碰巧可以很好地打印出来。例如,我如何获得 json.results.annotations.DMS.lat

But now json is just a type Any that happens to print nicely. How would I get , for example, json.results.annotations.DMS.lat?

推荐答案

这应该有所帮助:

AF.request(Places.uri, method: .get, parameters: parameters, encoding: URLEncoding(destination: .queryString)).responseString { response in
    switch response.result {

    case .success(let json):
        print(json)
        if let returnedValue = responseObject.result.value, !returnedValue.isEmpty {
            do {
                let locationObject = try JSONDecoder().decode(Location.self, from: (returnedValue.data(using: .utf8))!)
            } catch let e {
                print("Couldn't Parse data because... \(e)")
            }
        }
        DispatchQueue.main.async {

           callback(location)

        }
    case .failure(let error):
        print(error)

        callback(nil)
    }
}

这篇关于从AF ResponseJSON获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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