Swift 从AF responseJSON获取数据

Swift 从AF responseJSON获取数据,swift,alamofire,Swift,Alamofire,我有代码来处理位置数据,这样我可以提取可以匿名化数据的细节——例如,如果我的places API说它是一个_类型:“Building”和Building:“Safeway”——我可以将数据保存为lat/long的md5:“Safeway”,所有safeways在检查我的位置数据时看起来都是一样的。这也是我想要的 static func getLocationData(location: CLLocation, _ callback: @escaping (CLLocation?) -> V

我有代码来处理位置数据,这样我可以提取可以匿名化数据的细节——例如,如果我的places API说它是一个_类型:“Building”和Building:“Safeway”——我可以将数据保存为lat/long的md5:“Safeway”,所有safeways在检查我的位置数据时看起来都是一样的。这也是我想要的

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)
        }
    }
}
如我所见,此交易有效:

{
    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只是一种打印得很好的类型。例如,如何获得json.results.annotations.DMS.lat?,您可以试试

 if let res = json as? [String: Any]{
    if let inner = res["results"] as? [[String: Any]] {
        for item in inner {
            if let ert = item["annotations"] as? [[String: Any]] {
                for ann in ert {
                    print(ann["lat"])
                }
            }
        }
    }
}

你也能做到

struct Root: Codable {
    let results: [DataClass] 
}

struct DataClass: Codable {
    let annotations: [Anno]
}

struct Anno: Codable {
    let lat:Double // or String as in your question it's a String IDN if this is a description
}

        do {
             guard let data = try JSONSerialization.data(withJSONObject: json, options: []) else { return } 
            let locationObject = try JSONDecoder().decode(Root.self, from:data)
        } catch  {
            print(error)
        }
你可以试试

 if let res = json as? [String: Any]{
    if let inner = res["results"] as? [[String: Any]] {
        for item in inner {
            if let ert = item["annotations"] as? [[String: Any]] {
                for ann in ert {
                    print(ann["lat"])
                }
            }
        }
    }
}

你也能做到

struct Root: Codable {
    let results: [DataClass] 
}

struct DataClass: Codable {
    let annotations: [Anno]
}

struct Anno: Codable {
    let lat:Double // or String as in your question it's a String IDN if this is a description
}

        do {
             guard let data = try JSONSerialization.data(withJSONObject: json, options: []) else { return } 
            let locationObject = try JSONDecoder().decode(Root.self, from:data)
        } catch  {
            print(error)
        }
这将有助于:

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.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)
    }
}

如果topLevel=json as,是否执行
?[String:Any]{…}
一件一件地做。这是一本顶级词典。谢谢——哇,这还不算太好。应该有一种自动解包JSON的方法。如果您使用Swift 4+,请查找
Codable
来解析您的JSON。是的,我在其他地方见过Codable for JSON解析。。这可能是我使用的答案,但我并不迷恋这种方法。如何处理变量数据?如何处理混合阵列?应该有一组可组合的JSON类,可以用来模拟接收到的结构,而无需编写类型约束的有效负载定义——因为JSON没有您想要解析的ObjC/SWIFT那么受类型约束,那么为什么
Codable
不是一个好方法呢?最后需要一个表示它的结构/类。您可以使用JSONSerialization或Codable将JSON转换为您自己的结构/类。Swift 3.0或4.0不允许您执行“json.results.annotations.DMS.lat”。Objective-C可能有
json[@“结果”][0][@“注释”][@“DMS”][@“lat”]
,但不再是Swift。SwiftyJSON可能是复制这种“行为”的方法。如果topLevel=json为?[String:Any]{…}一件一件地做。这是一本顶级词典。谢谢——哇,这还不算太好。应该有一种自动解包JSON的方法。如果您使用Swift 4+,请查找
Codable
来解析您的JSON。是的,我在其他地方见过Codable for JSON解析。。这可能是我使用的答案,但我并不迷恋这种方法。如何处理变量数据?如何处理混合阵列?应该有一组可组合的JSON类,可以用来模拟接收到的结构,而无需编写类型约束的有效负载定义——因为JSON没有您想要解析的ObjC/SWIFT那么受类型约束,那么为什么
Codable
不是一个好方法呢?最后需要一个表示它的结构/类。您可以使用JSONSerialization或Codable将JSON转换为您自己的结构/类。Swift 3.0或4.0不允许您执行“json.results.annotations.DMS.lat”。Objective-C可能有
json[@“结果”][0][@“注释”][@“DMS”][@“lat”]
,但不再是Swift。SwiftyJSON可能是复制这种“行为”的一种方式。