简体   繁体   中英

downloading JSON for specific data from different endpoint

I am trying to get JSON image urls from a different endpoint. At the moment I am able to call first endpoint fetching data for exercise name, description and id. Then for each exercise I am trying to call to a different endpoint using an ID value so then I can get image url for the specific exercise.

The only idea I had is to create nested API call to a different endpoint, but I am getting too many syntax errors and it does not work.

The question is how can I reformat my code to remove existing syntax errors.

Here is my code. I never actually seen a way to do this type of API calls.

func parseData() {

    fetchedExercise.removeAll()

    let url = URL(string: urlPath)!

    let task = URLSession.shared.dataTask(with: url) { (data, response, error) in

        if error != nil {
            print("Error while parsing JSON")
        }
        else {

            do {
                if let data = data,
                    let fetchedData = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves) as? [String:Any],
                    let exercises = fetchedData["results"] as? [[String: Any]] {


                    for eachExercise in exercises {
                        if eachExercise["license_author"] as! String == "wger.de" {
                            let name = eachExercise["name"] as! String
                            let description = eachExercise["description"] as! String
                            let id = eachExercise["id"] as! Int



}

通过在末尾添加缺少的右括号来解决此问题,该右括号在尝试运行代码时导致语法错误。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM