iosjsonswiftafnetworking-3jsonparser

Get data from Feed


I am using AFNetworking for JSON parsing in my application.here is my response feed

{
    "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"
            }]
        }
    }
}

I want to get count and created field. I have tried but I am getting nil values.

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

Solution

  • May be you just forgot import AFNetworking?