-2

我在我的应用程序中使用 AFNetworking 进行 JSON 解析。这是我的响应提要

{
    "query": {
        "count": 1,
        "created": "2017-10-24T13:05:28Z",
        "lang": "en-US",
        "diagnostics": {
            "redirect": [{
                "from": "test.com",
                "status": "307",
                "content": "test.com"
            }, {
                "from": "test.com",
                "status": "307",
                "content": "test.com"
            }]
        }
    }
}

我想获得计数和创建的字段。我已经尝试过,但我得到了 nil 值。

func getJson() {
        let configuration = URLSessionConfiguration.default
        let manager = AFURLSessionManager(sessionConfiguration: configuration)
        let targetURL = URL(string: url)
        let request = URLRequest(url: targetURL!)
        let dataTask = manager.dataTask(with: request, completionHandler: {(_ response: URLResponse, _ data: Any, _ error: Error?) -> Void in

            guard let dict: [String: Any] = data as? [String : Any] else{
                return
            }
            print(dict)
            print(dict["count"] as? Int as Any)
            guard let dataString = dict["query"] as? [String : Any]  else{
                return
            }

            print(dataString)


        })
        dataTask.resume()
    }
4

1 回答 1

0

可能是您忘记了导入 AFNetworking?

于 2017-10-24T12:29:26.207 回答