【问题标题】:downloading JSON for specific data from different endpoint从不同端点下载特定数据的 JSON
【发布时间】:2017-11-26 02:00:27
【问题描述】:

我正在尝试从不同的端点获取 JSON 图像 URL。目前,我可以调用第一个端点来获取练习名称、描述和 ID 的数据。然后对于每个练习,我都尝试使用 ID 值调用不同的端点,这样我就可以获得特定练习的图像 url。

我唯一的想法是创建对不同端点的嵌套 API 调用,但是我遇到了太多语法错误并且它不起作用。

问题是如何重新格式化我的代码以消除现有的语法错误。

这是我的代码。我从未真正见过执行此类 API 调用的方法。

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



}

【问题讨论】:

  • 有什么问题?
  • 我在尝试将另一个 API 调用嵌套在第一个调用中时遇到语法错误。

标签: json swift api urlsession


【解决方案1】:

已通过在末尾添加缺少的闭括号来修复它,这会在尝试运行代码时导致语法错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-06
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    相关资源
    最近更新 更多