Swift 尝试解码json时得到Nil
我正在尝试从以下URL解码Json:Swift 尝试解码json时得到Nil,swift,Swift,我正在尝试从以下URL解码Json: https://api.letsbuildthatapp.com/appstore/featured 让我们从中构建该应用程序教程 这是我写的代码: import UIKit class AppCategory: NSObject{ var name: String? var apps: [App]? static func fetchedFeaturedApps(){ let jsonUrlString = "
https://api.letsbuildthatapp.com/appstore/featured
让我们从中构建该应用程序教程
这是我写的代码:
import UIKit
class AppCategory: NSObject{
var name: String?
var apps: [App]?
static func fetchedFeaturedApps(){
let jsonUrlString = "https://api.letsbuildthatapp.com/appstore/featured"
guard let url = URL(string: jsonUrlString) else {return}
URLSession.shared.dataTask(with: url) { (data, response, err) in
guard let data = data else {return}
do{
let featured = try JSONDecoder().decode(Featured.self, from: data)
}catch{
print(err)
}
}.resume()
}
}
struct Featured: Decodable {
var bannerCategory: Banner?
var featuredCategories: [mCategory]?
}
struct Banner: Decodable {
var name: String?
var apps: [String]?
var type: String?
}
struct mCategory: Decodable {
var name: String?
var apps: [App]?
var type: String?
}
struct App: Decodable {
var id: Int?
var name: String?
var category: String?
var price: Float?
var imageName: String?
}
我试着按照教程学习,但对我来说不起作用。
在尝试从url解码json时,我总是得到nil。我真的是个新手,不知道自己做错了什么。我知道要正确解码json,我需要有与json相同的结构,就像类别中的应用程序数组一样,但它仍然无法工作
编辑:应该提到,当我运行它时,代码进入catch块并打印nil
我尝试打印“err”,但日志中只显示以下内容:
2019-02-09 19:07:45.241000+0200 AppStore[2344:120273] [AXMediaCommon] Unable to look up screen scale
2019-02-09 19:07:45.241153+0200 AppStore[2344:120273] [AXMediaCommon] Unexpected physical screen orientation
2019-02-09 19:07:45.314112+0200 AppStore[2344:120273] [AXMediaCommon] Unable to look up screen scale
2019-02-09 19:07:45.319977+0200 AppStore[2344:120273] [AXMediaCommon] Unable to look up screen scale
2019-02-09 19:07:45.320189+0200 AppStore[2344:120273] [AXMediaCommon] Unexpected physical screen orientation
nil
你有
typeMismatchSwift.String,Swift.DecodingError.ContextcodingPath:[CodingKeyStringValue:bannerCategory,intValue:nil],debugDescription:应解码字符串,但找到了字典,UnderingError:nil
你需要
struct Featured: Codable {
let bannerCategory: BannerCategory
let categories: [Category]
}
struct BannerCategory: Codable {
let name: String
let apps: [BannerCategoryApp]
let type: String
}
struct BannerCategoryApp: Codable {
let imageName: String
enum CodingKeys: String, CodingKey {
case imageName = "ImageName"
}
}
struct Category: Codable {
let name: String
let apps: [CategoryApp]
let type: String
}
struct CategoryApp: Codable {
let id: Int?
let name, category: String?
let price: Double?
let imageName: String
enum CodingKeys: String, CodingKey {
case id = "Id"
case name = "Name"
case category = "Category"
case price = "Price"
case imageName = "ImageName"
}
}
你怎么知道是零?你有印刷特色吗?do/catch中是否有错误?它将进入catch并打印nil。我应该提一下。我现在要补充。很抱歉,因为存在PrintError,所以出现错误?你介意展示一下印刷品吗?因为里面有很多信息。我把日志添加到OP中,但我不认为这有什么帮助,因为它也只显示了nil。根据回答,没有,发现了一个错误。从哪里可以得到nil,下面你需要一个补全它直接进入catch块。请去掉所有的结构并用答案中的内容进行测试。我试图调试,这是它写的问题:debugDescription String没有与key codingkeystringvalue:\Name\,intValue:nil\Name\关联的值。好吧,刚刚再次检查,它现在正在工作,我猜问题在于JSON中的一些应用程序的价格为零,因为它是“免费应用”,所以CategoryApp中的价格是可选的。谢谢
class AppCategory: NSObject{
var name: String?
var apps: [CategoryApp]?
static func fetchedFeaturedApps(){
let jsonUrlString = "https://api.letsbuildthatapp.com/appstore/featured"
guard let url = URL(string: jsonUrlString) else {return}
URLSession.shared.dataTask(with: url) { (data, response, err) in
guard let data = data else {return}
do{
let featured = try JSONDecoder().decode(Featured.self, from: data)
print(featured)
}catch{
print(error)
}
}.resume()
}
}