API调用和;JSON解码
尝试执行API请求时遇到错误,随后将JSON解码到数据字段中 目标是在应用程序内查询API并将数据映射到字典中,以便按国家轻松查询数据 以下是API格式,不需要API密钥:API调用和;JSON解码,json,swift,api,dictionary,Json,Swift,Api,Dictionary,尝试执行API请求时遇到错误,随后将JSON解码到数据字段中 目标是在应用程序内查询API并将数据映射到字典中,以便按国家轻松查询数据 以下是API格式,不需要API密钥: { "Global": { "NewConfirmed": 98206, "TotalConfirmed": 5492996, "NewDeaths": 3030, "TotalDeaths": 351576, "NewRecovered": 56379, "TotalRe
{
"Global": {
"NewConfirmed": 98206,
"TotalConfirmed": 5492996,
"NewDeaths": 3030,
"TotalDeaths": 351576,
"NewRecovered": 56379,
"TotalRecovered": 2167913
},
"Countries": [
{
"Country": "Afghanistan",
"CountryCode": "AF",
"Slug": "afghanistan",
"NewConfirmed": 584,
"TotalConfirmed": 10582,
"NewDeaths": 2,
"TotalDeaths": 218,
"NewRecovered": 35,
"TotalRecovered": 1075,
"Date": "2020-05-26T03:04:40Z"
},
{
"Country": "Albania",
"CountryCode": "AL",
"Slug": "albania",
"NewConfirmed": 9,
"TotalConfirmed": 998,
"NewDeaths": 1,..........
以下是数据字段的结构,我认为这是我的问题所在,但我是一个初学者,不确定正确的结构/格式:
struct GlobalSum: Decodable {
let globalNC: Int
let globalTC: Int
let globalND: Int
let globalTD: Int
let globalNR: Int
let globalTR: Int
let sumByCntry: [CountrySum]
enum CodingKeys: String, CodingKey {
case globalNC = "NewConfirmed"
case globalTC = "TotalConfirmed"
case globalND = "NewDeaths"
case globalTD = "TotalDeaths"
case globalNR = "NewRecovered"
case globalTR = "TotalRecovered"
case sumByCntry = "Countries"
}
}
struct CountrySum: Decodable {
let Country: String
let CountryCode: String
let Slug: String
let NewConfirmed: Int
let TotalConfirmed: Int
let NewDeaths: Int
let TotalDeaths: Int
let NewRecovered: Int
let TotalRecovered: Int
let Date: String
}
下面是API调用和JSON解码:
func getSummary() {
let callString = "https://api.covid19api.com/summary"
let urlCall = URL(string: callString)
guard urlCall != nil else {
print("Error reaching API")
return
}
let session = URLSession.shared
let dataTask = session.dataTask(with: urlCall!) { (data, response, error) in
if error == nil && data != nil {
let decoder = JSONDecoder()
do {
let sumPull = try decoder.decode(GlobalSum.self, from: data!)
print(sumPull.globalNC)
}
catch {
print(error) }
}
}
dataTask.resume()
}
下面是错误:
dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.})))
如有任何建议,将不胜感激。为可能明显的错误道歉。刚刚开始在Swift上进行自我教育。我从json获得的信息。。你的语气应该是这样的
struct CoronaCases {
let global : GlobalSum
let countries: CountrySum
let date: String
enum CodingKeys: String, CodingKey {
case global = "Global"
case countries = "Countries"
case date = "Date"
}
}
struct GlobalSum: Decodable {
let globalNC: Int
let globalTC: Int
let globalND: Int
let globalTD: Int
let globalNR: Int
let globalTR: Int
enum CodingKeys: String, CodingKey {
case globalNC = "NewConfirmed"
case globalTC = "TotalConfirmed"
case globalND = "NewDeaths"
case globalTD = "TotalDeaths"
case globalNR = "NewRecovered"
case globalTR = "TotalRecovered"
}
}
struct CountrySum: Decodable {
let Country: String
let CountryCode: String
let Slug: String
let NewConfirmed: Int
let TotalConfirmed: Int
let NewDeaths: Int
let TotalDeaths: Int
let NewRecovered: Int
let TotalRecovered: Int
let Date: String
}
然后
let sumPull = try decoder.decode(CoronaCases.self, from: data!)
下面是app.quicktype.io生成的内容
import Foundation
// MARK: - Welcome
struct Welcome: Codable {
let global: Global
let countries: [Country]
let date: Date
enum CodingKeys: String, CodingKey {
case global = "Global"
case countries = "Countries"
case date = "Date"
}
}
// MARK: - Country
struct Country: Codable {
let country, countryCode, slug: String
let newConfirmed, totalConfirmed, newDeaths, totalDeaths: Int
let newRecovered, totalRecovered: Int
let date: Date
enum CodingKeys: String, CodingKey {
case country = "Country"
case countryCode = "CountryCode"
case slug = "Slug"
case newConfirmed = "NewConfirmed"
case totalConfirmed = "TotalConfirmed"
case newDeaths = "NewDeaths"
case totalDeaths = "TotalDeaths"
case newRecovered = "NewRecovered"
case totalRecovered = "TotalRecovered"
case date = "Date"
}
}
// MARK: - Global
struct Global: Codable {
let newConfirmed, totalConfirmed, newDeaths, totalDeaths: Int
let newRecovered, totalRecovered: Int
enum CodingKeys: String, CodingKey {
case newConfirmed = "NewConfirmed"
case totalConfirmed = "TotalConfirmed"
case newDeaths = "NewDeaths"
case totalDeaths = "TotalDeaths"
case newRecovered = "NewRecovered"
case totalRecovered = "TotalRecovered"
}
}
我从json获得的信息。。你的语气应该是这样的
struct CoronaCases {
let global : GlobalSum
let countries: CountrySum
let date: String
enum CodingKeys: String, CodingKey {
case global = "Global"
case countries = "Countries"
case date = "Date"
}
}
struct GlobalSum: Decodable {
let globalNC: Int
let globalTC: Int
let globalND: Int
let globalTD: Int
let globalNR: Int
let globalTR: Int
enum CodingKeys: String, CodingKey {
case globalNC = "NewConfirmed"
case globalTC = "TotalConfirmed"
case globalND = "NewDeaths"
case globalTD = "TotalDeaths"
case globalNR = "NewRecovered"
case globalTR = "TotalRecovered"
}
}
struct CountrySum: Decodable {
let Country: String
let CountryCode: String
let Slug: String
let NewConfirmed: Int
let TotalConfirmed: Int
let NewDeaths: Int
let TotalDeaths: Int
let NewRecovered: Int
let TotalRecovered: Int
let Date: String
}
然后
let sumPull = try decoder.decode(CoronaCases.self, from: data!)
下面是app.quicktype.io生成的内容
import Foundation
// MARK: - Welcome
struct Welcome: Codable {
let global: Global
let countries: [Country]
let date: Date
enum CodingKeys: String, CodingKey {
case global = "Global"
case countries = "Countries"
case date = "Date"
}
}
// MARK: - Country
struct Country: Codable {
let country, countryCode, slug: String
let newConfirmed, totalConfirmed, newDeaths, totalDeaths: Int
let newRecovered, totalRecovered: Int
let date: Date
enum CodingKeys: String, CodingKey {
case country = "Country"
case countryCode = "CountryCode"
case slug = "Slug"
case newConfirmed = "NewConfirmed"
case totalConfirmed = "TotalConfirmed"
case newDeaths = "NewDeaths"
case totalDeaths = "TotalDeaths"
case newRecovered = "NewRecovered"
case totalRecovered = "TotalRecovered"
case date = "Date"
}
}
// MARK: - Global
struct Global: Codable {
let newConfirmed, totalConfirmed, newDeaths, totalDeaths: Int
let newRecovered, totalRecovered: Int
enum CodingKeys: String, CodingKey {
case newConfirmed = "NewConfirmed"
case totalConfirmed = "TotalConfirmed"
case newDeaths = "NewDeaths"
case totalDeaths = "TotalDeaths"
case newRecovered = "NewRecovered"
case totalRecovered = "TotalRecovered"
}
}
像这样更新您的模型并尝试解析它。这是我的密码 以下是正确的结构: <代码>导入基础 结构摘要:可编码{ 让全球:全球 让国家:[国家] 让日期:字符串 枚举编码键:字符串,编码键{ case global=“global” 案例国家=“国家” 案例日期=“日期” } } 结构国家:可编码{ let country,countryCode,slug:String 让newconfirm,totalconfirm,newdeath,totaldeath:Int 让newRecovered,totalRecovered:Int 让日期:字符串 枚举编码键:字符串,编码键{ 案例国家=“国家” 案例countryCode=“countryCode” case slug=“slug” 案例newconfirm=“newconfirm” 病例总数已确认=“总数已确认” 案例newDeaths=“newDeaths” 案例总死亡数=“总死亡数” 案例newRecovered=“newRecovered” 案例totalRecovered=“totalRecovered” 案例日期=“日期” } } 结构全局:可编码{ 让newconfirm,totalconfirm,newdeath,totaldeath:Int 让newRecovered,totalRecovered:Int 枚举编码键:字符串,编码键{ 案例newconfirm=“newconfirm” 病例总数已确认=“总数已确认” 案例newDeaths=“newDeaths” 案例总死亡数=“总死亡数” 案例newRecovered=“newRecovered” 案例totalRecovered=“totalRecovered” } } 下面是API调用和JSON解码:
func getSummary() {
let callString = "https://api.covid19api.com/summary"
let urlCall = URL(string: callString)
guard urlCall != nil else {
print("Error reaching API")
return
}
let session = URLSession.shared
let dataTask = session.dataTask(with: urlCall!) { (data, response, error) in
if error == nil && data != nil {
let decoder = JSONDecoder()
do {
let sumPull = try decoder.decode(GlobalSum.self, from: data!)
print(sumPull.globalNC)
}
catch {
print(error) }
}
}
dataTask.resume()
}
func getSummary(){
让调用字符串=”https://api.covid19api.com/summary"
让urlCall=URL(字符串:callString)
守卫呼叫!=无其他呼叫{
打印(“到达API时出错”)
返回
}
让session=URLSession.shared
让dataTask=session.dataTask(带:urlCall!){(数据、响应、错误)在
如果错误==nil&&data!=nil{
let decoder=JSONDecoder()
做{
让sumPull=try decoder.decode(Summary.self,from:data!)
打印(sumPull.global)
}
抓住{
打印(错误)
}
}
}
dataTask.resume()
}
像这样更新您的模型并尝试解析它。这是我的密码
以下是正确的结构:
<代码>导入基础
结构摘要:可编码{
让全球:全球
让国家:[国家]
让日期:字符串
枚举编码键:字符串,编码键{
case global=“global”
案例国家=“国家”
案例日期=“日期”
}
}
结构国家:可编码{
let country,countryCode,slug:String
让newconfirm,totalconfirm,newdeath,totaldeath:Int
让newRecovered,totalRecovered:Int
让日期:字符串
枚举编码键:字符串,编码键{
案例国家=“国家”
案例countryCode=“countryCode”
case slug=“slug”
案例newconfirm=“newconfirm”
病例总数已确认=“总数已确认”
案例newDeaths=“newDeaths”
案例总死亡数=“总死亡数”
案例newRecovered=“newRecovered”
案例totalRecovered=“totalRecovered”
案例日期=“日期”
}
}
结构全局:可编码{
让newconfirm,totalconfirm,newdeath,totaldeath:Int
让newRecovered,totalRecovered:Int
枚举编码键:字符串,编码键{
案例newconfirm=“newconfirm”
病例总数已确认=“总数已确认”
案例newDeaths=“newDeaths”
案例总死亡数=“总死亡数”
案例newRecovered=“newRecovered”
案例totalRecovered=“totalRecovered”
}
}
下面是API调用和JSON解码:
func getSummary() {
let callString = "https://api.covid19api.com/summary"
let urlCall = URL(string: callString)
guard urlCall != nil else {
print("Error reaching API")
return
}
let session = URLSession.shared
let dataTask = session.dataTask(with: urlCall!) { (data, response, error) in
if error == nil && data != nil {
let decoder = JSONDecoder()
do {
let sumPull = try decoder.decode(GlobalSum.self, from: data!)
print(sumPull.globalNC)
}
catch {
print(error) }
}
}
dataTask.resume()
}
func getSummary(){
让调用字符串=”https://api.covid19api.com/summary"
让urlCall=URL(字符串:callString)
守卫呼叫!=无其他呼叫{
打印(“到达API时出错”)
返回
}
让session=URLSession.shared
让dataTask=session.dataTask(带:urlCall!){(数据、响应、错误)在
如果错误==nil&&data!=nil{
let decoder=JSONDecoder()
做{
让sumPull=try decoder.decode(Summary.self,from:data!)
打印(sumPull.global)
}
抓住{
打印(错误)
}
}
}
dataTask.resume()
}
请发布完整的json?将您的json粘贴到当前返回“您已达到最大请求限制”的端点,这确实是无效的json请发布完整的json?将您的json粘贴到当前返回“您已达到最大请求限制”的端点,这确实是无效的json