diff --git a/MartHoliday/MartHoliday/Utility/Extensions.swift b/MartHoliday/MartHoliday/Utility/Extensions.swift index 2f715f7..77f77f6 100644 --- a/MartHoliday/MartHoliday/Utility/Extensions.swift +++ b/MartHoliday/MartHoliday/Utility/Extensions.swift @@ -205,3 +205,16 @@ extension UIDevice { } } + +extension JSONDecoder { + func decode(_ type: T.Type, from data: Data, keyPath: String) throws -> T { + let toplevel = try JSONSerialization.jsonObject(with: data) + if let nestedJson = (toplevel as AnyObject).value(forKeyPath: keyPath) { + let nestedJsonData = try JSONSerialization.data(withJSONObject: nestedJson) + return try decode(type, from: nestedJsonData) + } else { + throw DecodingError.dataCorrupted(.init(codingPath: [], debugDescription: "Nested json not found for key path \"\(keyPath)\"")) + } + } +} + diff --git a/MartHoliday/MartHoliday/ViewController/MainViewController.swift b/MartHoliday/MartHoliday/ViewController/MainViewController.swift index 7936134..c809f50 100644 --- a/MartHoliday/MartHoliday/ViewController/MainViewController.swift +++ b/MartHoliday/MartHoliday/ViewController/MainViewController.swift @@ -147,8 +147,9 @@ class MainViewController: RechabilityDetectViewController, FavoriteConvertible, let ids = FavoriteList.shared().ids() let idstr = ids.map{String($0)}.joined(separator: ",") guard let urlstr = KeyInfoLoader.loadValue(of: .FavoriteBranchesURL) else { return } - guard let baseURL = URL(string: urlstr) else { return } - let url = baseURL.appendingPathComponent(idstr) + var urlComp = URLComponents(string: urlstr) + urlComp?.queryItems = [URLQueryItem(name: "ids", value: idstr)] + guard let url = urlComp?.url else {return} let configure = URLSessionConfiguration.default configure.timeoutIntervalForRequest = 3 @@ -160,7 +161,7 @@ class MainViewController: RechabilityDetectViewController, FavoriteConvertible, var favoriteList = BranchList() var mainFavorites = [FavoriteBranch]() do { - branches = try JSONDecoder().decode([BranchRawData].self, from: data) + branches = try JSONDecoder().decode([BranchRawData].self, from: data, keyPath: "data") favoriteList = BranchList(branches: branches) for fav in favoriteList.branches {