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

2

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.