0

I am developing an using the JSON .So i am new to this field. I need to get the values from the url and the pattern is as below mention:- {Data:[{name:RAHUL,place:WORLD,location:[{city:WORLD}]},{},{},{}],mata:{},link:{}}

I need the "Data" value as the pattern array.That is i need to get the Data values seperate. How to get the values name and city values from this

Dixit Akabari
  • 2,419
  • 13
  • 26
abel
  • 11
  • 4

1 Answers1

1

Try this way:

let sessionConfig = URLSessionConfiguration.default

        // Create session, and optionally set a URLSessionDelegate
        let session = URLSession(configuration: sessionConfig, delegate: nil, delegateQueue: nil)

        // Create request
        guard let URL = URL(string: "YOUR_URL_HERE") else { return }
        let request = URLRequest(url: URL)

        let task = session.dataTask(with: request) { (data, response, err) in
            if (err == nil) {
                // Success
                let statusCode = (response as! HTTPURLResponse).statusCode
                print("URL Session Task Succeeded: HTTP \(statusCode)")
                if let data = data {

                    do {
                        let jsonResult = try JSONSerialization.jsonObject(with: data, options: .mutableContainers)

                        // Parse JSON data
                        if let json = jsonResult as? [String:Any],
                        let dataArr = json["Data"] as? [[String:Any]] {

                            for item in dataArr {

                                let name = item["name"] as! String
                                // AND SO ON PARSE ANOTHER FIELDS
                            }
                        }

                    } catch let err {
                        print(err)
                    }

                }
            } else {
                // Failure
                print("URL Session Task Failed: \(err!.localizedDescription)")
            }
        }
        task.resume()
        session.finishTasksAndInvalidate()
Anton Novoselov
  • 769
  • 2
  • 7
  • 19