从Feed获取数据

时间:2017-10-24 12:25:33

标签: ios json swift afnetworking-3 jsonparser

我在我的应用程序中使用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"
            }]
        }
    }
}

我想要计算和创建字段。 我试过了,但我得到的是零值。

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

1 个答案:

答案 0 :(得分:0)

可能你刚忘了导入AFNetworking?