【问题标题】:Swift - Parse JSON using Alamofire from PHP dataSwift - 使用来自 PHP 数据的 Alamofire 解析 JSON
【发布时间】:2018-04-20 17:45:51
【问题描述】:

我正在尝试使用 PHP 为中国用户创建一个适配器来连接 GoogleAPI。在 json 响应中,我试图获取描述数据。但是,我什至无法获取字典数据。有什么建议吗?

iOS 代码

Alamofire.request(requestString, method: .post, parameters: data, encoding: URLEncoding.httpBody, headers: [:]).responseJSON { (response) in
    switch response.result {
    case .success(_):
        print(response.result.value)
        if let dictionary = response.result.value as? Dictionary<String,Any>{
            print("dictionary", dictionary)
        }
    }
}

PHP

<?php
$post_data = $_POST['data'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://vision.googleapis.com/v1/images:annotate?key=KKEEYY");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-length: ".strlen($post_data)));
$result = curl_exec($ch);
echo json_encode($result);
?>

编辑

如果我使用自己的适配器,我可以看到键和值有额外的“”

如果直接向 Google print("direct", response.result.value)请求响应

如果使用我的适配器,请回复print("using adapter", response.result.value)

【问题讨论】:

    标签: php ios json swift alamofire


    【解决方案1】:

    您的结果已经是字典,无需使用 NSDictionary 进行转换。此外,您必须使用 swift 类,避免在 Swift 中使用 Objective-c 类:

    试试这个:

    switch response.result {
           case .success:
             if let jsonDict = response.result.value as? Dictionary<String,Any> {
                print("Json Response: \(jsonDict)") // serialized json response
                 if let responses = jsonDict["responses"] as? [[String:Any]] {
                    print("3", responses)
                 }
             }
             if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
                print("Server Response: \(utf8Text)") // original server data as UTF8 string
             }
             break
           case .failure(let error):
                print(error)
                break
             }
    }
    

    【讨论】:

    • resultDict的变量?
    • 再检查一遍。
    • 结果 - 类型 'Any' 没有下标成员
    • 更新了看看。
    • print("1", result) 不打印任何东西
    【解决方案2】:

    为方便起见,最好使用 swifty JSON 并将其与 alamofire 结合使用

    【讨论】:

    • 您介意看看我的编辑吗?我发现使用自己的适配器和直接向 Google 请求之间存在差异
    【解决方案3】:

    希望这会有所帮助..

    let jsonData = response.data
    let dictionary = try? JSONSerialization.jsonObject(with: jsonData!, options: []) as? [String : Any]
    print("dictionary", dictionary)
    

    【讨论】:

    • 我的尝试离你很近。尝试if let dictionary = response.result.value as? String { let data: NSData = dictionary.data(using: String.Encoding.utf8)! as NSData let json = JSON(data) print("JSON", json) print("JSON1", json["responses"])我可以看到JSON1不是nil
    【解决方案4】:

    这就是答案。我知道这太乱了。

    if let dictionary = response.result.value as? String {
        let data: NSData = dictionary.data(using: String.Encoding.utf8)! as NSData
        let json = JSON(data)
        let responses = json["responses"].arrayObject
        if let resp = responses!.first {
            let type = resp as! [String: Any]
            if let labelAnnotations = type["labelAnnotations"] as? NSArray {
                for labelAnnotation in labelAnnotations {
                    if let results = labelAnnotation as? NSDictionary {
                        let description = results["description"] as? String
                        let score = results["score"] as? Double
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-12
      • 2020-11-19
      • 1970-01-01
      • 1970-01-01
      • 2019-04-15
      • 2020-02-23
      • 1970-01-01
      • 2019-03-19
      相关资源
      最近更新 更多