从Yelp API获得JSON响应,但解码后返回nil

时间:2019-07-11 13:42:02

标签: ios json swift yelp

我正在尝试解析JSON以显示来自Yelp API事件搜索的事件

我尝试更改API请求和参数,打开Yelp beta开发人员模式,得到响应,并看到它由12000bits组成,但是在解码打印(root)后返回零响应。我正在Swift上进行编程

private func loadBusinesses(with coordinate: CLLocationCoordinate2D) {
        service.request(.search(lat: coordinate.latitude, long: coordinate.longitude)) { [weak self] (result) in
            guard let strongSelf = self else { return }
            switch result {
            case .success(let response):
               // print(try? JSONSerialization.jsonObject(with: response.data, options: []))
                let root = try? self?.jsonDecoder.decode(Root.self, from: response.data)
                print(root)
                //print(coordinate.latitude)
                let viewModels = root??.businesses
                    .compactMap(RestaurantListViewModel.init)
                  //  .sorted(by: { $0.distance < $1.distance })
                if let nav = strongSelf.window.rootViewController as? UINavigationController,
                    let restaurantListViewController = nav.topViewController as? RestaurantTableViewController {
                    restaurantListViewController.viewModels = viewModels ?? []
                } else if let nav = strongSelf.storyboard
                    .instantiateViewController(withIdentifier: "RestaurantNavigationController") as? UINavigationController {
                    strongSelf.navigationController = nav
                    strongSelf.window.rootViewController?.present(nav, animated: true) {
                        (nav.topViewController as? RestaurantTableViewController)?.delegete = self
                        (nav.topViewController as? RestaurantTableViewController)?.viewModels = viewModels ?? []
                    }
                }
            case .failure(let error):
                print("Error: \(error)")
            }
        }
    }

解码JSON

0 个答案:

没有答案