- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这些是我创建的结构
struct main : Decodable{ // these are the values I want to retrieve
let catname : String?
let catid : String?
let sub_title : String?
let image : String?
let contest : [contest]
enum CodingKeys : String , CodingKey {
case catname = "catname"
case catid = "catid"
case sub_title = "sub_title"
case image = "image"
case contest = "contest"
}
}
struct contest : Decodable {
let id : Int
let name : String
let entryfee : Int
let contest_type : String
let winning_percentage : Int
let win_amount : Int
let matchkey : String
let c_type : String
let status : Int
let joinedusers : Int
let maximum_user : Int
let getjoinedpercentage : String
let multi_entry : Int
let confirmed_challenge : Int
let is_running : Int
let is_bonus : Int
let bonus_percentage : Int
let isselected : Bool
let isselectedid : String
let refercode : String
var price_card : [price_card]
let totalwinners : Int
enum CodingKeys : String, CodingKey{
case id , name , entryfee , contest_type , winning_percentage ,win_amount,matchkey,c_type,status,joinedusers,maximum_user,getjoinedpercentage,multi_entry,confirmed_challenge,is_running,is_bonus,bonus_percentage,isselected,isselectedid,refercode,price_card,totalwinners
}
}
struct price_card : Decodable {
let price : String
let winners : Int
let start_position :String
enum CodingKeys : String, CodingKey{
case price , winners , start_position
}
这是解析数据的主要代码。我在运行时遇到的错误是 Error keyNotFound(CodingKeys(stringValue: "catid", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription : "No value associated with key CodingKeys(stringValue:\"catid\", intValue: nil) (\"catid\").", underlyingError: nil))
func experiment1(){
let Url = URL(string: url)
var request = URLRequest(url: Url!)
request.httpMethod = "POST"
request.setValue("bearer 03a4013d77568a67c159a41f680d14f4",forHTTPHeaderField: "Authorization")
URLSession.shared.dataTask(with: Url!){ data,response,error
in
guard error == nil else{
print("some error is found",error)
return
}
guard let data = data else {
return
}
print(data)
do{
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let Main = try decoder.decode(main.self,from: data)
print(Main.catid)
}
catch
let jsonErr {
print("Error" , jsonErr)
}
}.resume()
}
这是我的JSON
[{
"catname": "HOT CONTESTS",
"catid": 4,
"sub_title": "Call the shot quickly",
"image": "https://mybat11.com/admin/public/images_contest_category/contest_1572688096.jpg",
"contest": [
{
"id": 73,
"name": "Win Rs.1000",
"entryfee": 12,
"contest_type": "Amount",
"winning_percentage": 0,
"win_amount": 1000,
"matchkey": "bplt20_2020_g27",
"c_type": "classic",
"status": 1,
"joinedusers": 1,
"maximum_user": 100,
"getjoinedpercentage": "1%",
"multi_entry": 1,
"confirmed_challenge": 0,
"is_running": 0,
"is_bonus": 0,
"bonus_percentage": 0,
"multientry_limit": null,
"isselected": false,
"isselectedid": "",
"refercode": "",
"price_card": [
{
"price": "Rs.1000",
"winners": 1,
"start_position": "1"
}
],
"totalwinners": 1
},
{
"id": 77,
"name": "Win Rs.1000",
"entryfee": 12,
"contest_type": "Amount",
"winning_percentage": 0,
"win_amount": 1000,
"matchkey": "bblt20_2019_g17",
"c_type": "classic",
"status": 1,
"joinedusers": 0,
"maximum_user": 100,
"getjoinedpercentage": "0%",
"multi_entry": 1,
"confirmed_challenge": 0,
"is_running": 0,
"is_bonus": 0,
"bonus_percentage": 0,
"multientry_limit": null,
"isselected": false,
"isselectedid": "",
"refercode": "",
"price_card": [
{
"price": "Rs.1000",
"winners": 1,
"start_position": "1"
}
],
"totalwinners": 1
},
{
"id": 81,
"name": "Win Rs.1000",
"entryfee": 12,
"contest_type": "Amount",
"winning_percentage": 0,
"win_amount": 1000,
"matchkey": "c.match.adh_vs_cm.c028d",
"c_type": "classic",
"status": 1,
"joinedusers": 0,
"maximum_user": 100,
"getjoinedpercentage": "0%",
"multi_entry": 1,
"confirmed_challenge": 0,
"is_running": 0,
"is_bonus": 0,
"bonus_percentage": 0,
"multientry_limit": null,
"isselected": false,
"isselectedid": "",
"refercode": "",
"price_card": [
{
"price": "Rs.1000",
"winners": 1,
"start_position": "1"
}
],
"totalwinners": 1
},
{
"id": 85,
"name": "Win Rs.1000",
"entryfee": 12,
"contest_type": "Amount",
"winning_percentage": 0,
"win_amount": 1000,
"matchkey": "c.match.ns_vs_wb.63191",
"c_type": "classic",
"status": 1,
"joinedusers": 0,
"maximum_user": 100,
"getjoinedpercentage": "0%",
"multi_entry": 1,
"confirmed_challenge": 0,
"is_running": 0,
"is_bonus": 0,
"bonus_percentage": 0,
"multientry_limit": null,
"isselected": false,
"isselectedid": "",
"refercode": "",
"price_card": [
{
"price": "Rs.1000",
"winners": 1,
"start_position": "1"
}
],
"totalwinners": 1
},
{
"id": 89,
"name": "Win Rs.1000",
"entryfee": 12,
"contest_type": "Amount",
"winning_percentage": 0,
"win_amount": 1000,
"matchkey": "sst20_2019_g17",
"c_type": "classic",
"status": 1,
"joinedusers": 0,
"maximum_user": 100,
"getjoinedpercentage": "0%",
"multi_entry": 1,
"confirmed_challenge": 0,
"is_running": 0,
"is_bonus": 0,
"bonus_percentage": 0,
"multientry_limit": null,
"isselected": false,
"isselectedid": "",
"refercode": "",
"price_card": [
{
"price": "Rs.1000",
"winners": 1,
"start_position": "1"
}
],
"totalwinners": 1
},
{
"id": 93,
"name": "Win Rs.1000",
"entryfee": 12,
"contest_type": "Amount",
"winning_percentage": 0,
"win_amount": 1000,
"matchkey": "sst20_2019_g18",
"c_type": "classic",
"status": 1,
"joinedusers": 0,
"maximum_user": 100,
"getjoinedpercentage": "0%",
"multi_entry": 1,
"confirmed_challenge": 0,
"is_running": 0,
"is_bonus": 0,
"bonus_percentage": 0,
"multientry_limit": null,
"isselected": false,
"isselectedid": "",
"refercode": "",
"price_card": [
{
"price": "Rs.1000",
"winners": 1,
"start_position": "1"
}
],
"totalwinners": 1
},
{
"id": 97,
"name": "Win Rs.1000",
"entryfee": 12,
"contest_type": "Amount",
"winning_percentage": 0,
"win_amount": 1000,
"matchkey": "bblt20_2019_g18",
"c_type": "classic",
"status": 1,
"joinedusers": 0,
"maximum_user": 100,
"getjoinedpercentage": "0%",
"multi_entry": 1,
"confirmed_challenge": 0,
"is_running": 0,
"is_bonus": 0,
"bonus_percentage": 0,
"multientry_limit": null,
"isselected": false,
"isselectedid": "",
"refercode": "",
"price_card": [
{
"price": "Rs.1000",
"winners": 1,
"start_position": "1"
}
],
"totalwinners": 1
},
{
"id": 101,
"name": "Win Rs.1000",
"entryfee": 12,
"contest_type": "Amount",
"winning_percentage": 0,
"win_amount": 1000,
"matchkey": "c.match.ch_vs_os.c6d23",
"c_type": "classic",
"status": 1,
"joinedusers": 0,
"maximum_user": 100,
"getjoinedpercentage": "0%",
"multi_entry": 1,
"confirmed_challenge": 0,
"is_running": 0,
"is_bonus": 0,
"bonus_percentage": 0,
"multientry_limit": null,
"isselected": false,
"isselectedid": "",
"refercode": "",
"price_card": [
{
"price": "Rs.1000",
"winners": 1,
"start_position": "1"
}
],
"totalwinners": 1
},
{
"id": 105,
"name": "Win Rs.1000",
"entryfee": 12,
"contest_type": "Amount",
"winning_percentage": 0,
"win_amount": 1000,
"matchkey": "bblt20_2019_g19",
"c_type": "classic",
"status": 1,
"joinedusers": 0,
"maximum_user": 100,
"getjoinedpercentage": "0%",
"multi_entry": 1,
"confirmed_challenge": 0,
"is_running": 0,
"is_bonus": 0,
"bonus_percentage": 0,
"multientry_limit": null,
"isselected": false,
"isselectedid": "",
"refercode": "",
"price_card": [
{
"price": "Rs.1000",
"winners": 1,
"start_position": "1"
}
],
"totalwinners": 1
},
{
"id": 109,
"name": "Win Rs.1000",
"entryfee": 12,
"contest_type": "Amount",
"winning_percentage": 0,
"win_amount": 1000,
"matchkey": "sst20_2019_g19",
"c_type": "classic",
"status": 1,
"joinedusers": 0,
"maximum_user": 100,
"getjoinedpercentage": "0%",
"multi_entry": 1,
"confirmed_challenge": 0,
"is_running": 0,
"is_bonus": 0,
"bonus_percentage": 0,
"multientry_limit": null,
"isselected": false,
"isselectedid": "",
"refercode": "",
"price_card": [
{
"price": "Rs.1000",
"winners": 1,
"start_position": "1"
}
],
"totalwinners": 1
},
{
"id": 113,
"name": "Win Rs.1000",
"entryfee": 12,
"contest_type": "Amount",
"winning_percentage": 0,
"win_amount": 1000,
"matchkey": "bplt20_2020_g29",
"c_type": "classic",
"status": 1,
"joinedusers": 0,
"maximum_user": 100,
"getjoinedpercentage": "0%",
"multi_entry": 1,
"confirmed_challenge": 0,
"is_running": 0,
"is_bonus": 0,
"bonus_percentage": 0,
"multientry_limit": null,
"isselected": false,
"isselectedid": "",
"refercode": "",
"price_card": [
{
"price": "Rs.1000",
"winners": 1,
"start_position": "1"
}
],
"totalwinners": 1
},
{
"id": 117,
"name": "Win Rs.1000",
"entryfee": 12,
"contest_type": "Amount",
"winning_percentage": 0,
"win_amount": 1000,
"matchkey": "bblt20_2019_g20",
"c_type": "classic",
"status": 1,
"joinedusers": 0,
"maximum_user": 100,
"getjoinedpercentage": "0%",
"multi_entry": 1,
"confirmed_challenge": 0,
"is_running": 0,
"is_bonus": 0,
"bonus_percentage": 0,
"multientry_limit": null,
"isselected": false,
"isselectedid": "",
"refercode": "",
"price_card": [
{
"price": "Rs.1000",
"winners": 1,
"start_position": "1"
}
],
"totalwinners": 1
},
{
"id": 121,
"name": "Win Rs.1000",
"entryfee": 12,
"contest_type": "Amount",
"winning_percentage": 0,
"win_amount": 1000,
"matchkey": "bplt20_2020_g30",
"c_type": "classic",
"status": 1,
"joinedusers": 0,
"maximum_user": 100,
"getjoinedpercentage": "0%",
"multi_entry": 1,
"confirmed_challenge": 0,
"is_running": 0,
"is_bonus": 0,
"bonus_percentage": 0,
"multientry_limit": null,
"isselected": false,
"isselectedid": "",
"refercode": "",
"price_card": [
{
"price": "Rs.1000",
"winners": 1,
"start_position": "1"
}
],
"totalwinners": 1
},
{
"id": 125,
"name": "Win Rs.1000",
"entryfee": 12,
"contest_type": "Amount",
"winning_percentage": 0,
"win_amount": 1000,
"matchkey": "ausnz_2019_test_03",
"c_type": "classic",
"status": 1,
"joinedusers": 0,
"maximum_user": 100,
"getjoinedpercentage": "0%",
"multi_entry": 1,
"confirmed_challenge": 0,
"is_running": 0,
"is_bonus": 0,
"bonus_percentage": 0,
"multientry_limit": null,
"isselected": false,
"isselectedid": "",
"refercode": "",
"price_card": [
{
"price": "Rs.1000",
"winners": 1,
"start_position": "1"
}
],
"totalwinners": 1
},
{
"id": 129,
"name": "Win Rs.1000",
"entryfee": 12,
"contest_type": "Amount",
"winning_percentage": 0,
"win_amount": 1000,
"matchkey": "c.match.wb_vs_ns.7fdc4",
"c_type": "classic",
"status": 1,
"joinedusers": 0,
"maximum_user": 100,
"getjoinedpercentage": "0%",
"multi_entry": 1,
"confirmed_challenge": 0,
"is_running": 0,
"is_bonus": 0,
"bonus_percentage": 0,
"multientry_limit": null,
"isselected": false,
"isselectedid": "",
"refercode": "",
"price_card": [
{
"price": "Rs.1000",
"winners": 1,
"start_position": "1"
}
],
"totalwinners": 1
},
{
"id": 133,
"name": "Win Rs.1000",
"entryfee": 12,
"contest_type": "Amount",
"winning_percentage": 0,
"win_amount": 1000,
"matchkey": "sst20_2019_g20",
"c_type": "classic",
"status": 1,
"joinedusers": 0,
"maximum_user": 100,
"getjoinedpercentage": "0%",
"multi_entry": 1,
"confirmed_challenge": 0,
"is_running": 0,
"is_bonus": 0,
"bonus_percentage": 0,
"multientry_limit": null,
"isselected": false,
"isselectedid": "",
"refercode": "",
"price_card": [
{
"price": "Rs.1000",
"winners": 1,
"start_position": "1"
}
],
"totalwinners": 1
}
]
}
]
最佳答案
let catid : String?
应该是 let catid : Int?
let Main = try decoder.decode(main.self,from: data)
应该是 let Main = try decoder.decode([main].self,from: data)
我已经修复它并且一切正常。我想建议删除这个:
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let Main = try decoder.decode(main.self,from: data)
print(Main.catid)
然后只添加它:
let Main = try? JSONDecoder().decode([main].self, from: data)
print(Main?.first?.catid)
在我这边,另外,我推荐阅读 the Apple documentation关于命名类和变量。
关于ios - 如何解析嵌套的 JSON。我得到 nil 值作为输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59540354/
在检查对象是否为 nil 时,有人使用 1: if (object == nil) { //... } 有人用 2: if (nil == object) { //... } 1和2有
func InsertApData(w http.ResponseWriter, r *http.Request) { decoder := json.NewDecoder(r.Body)
为什么 nil.to_s 返回 "",而 nil.inspect 返回 "nil"(当显然 .inspect 使用 .to_s 方法) 最佳答案 "inspect" method of the Obj
运行时间 https://play.golang.org/p/sl12vfS9vP package main import "fmt" func main() { err := run()
我可以将三元条件运算符用于 if {} else {} 语句,如下所示:a ? x : y,还是问题?回答 1 : 回答 2。 是否可以使用这种格式来检查,而不是 a 是 true 还是 false,
无法弄清楚这里出了什么问题。按照设计设置说明,用谷歌搜索我能想到的一切,仍然没有运气。 undefined method `email' for # Extracted source (around
今天写一些 rspec 时,我遇到了一些意外的行为,将日期(和时间)实例与 nil 进行比较。这是一个使用原始 ruby 的示例(没有 Rails 或其他库): user@MacBook-Work
我将数据类型的非零值分配给非可选属性,然后将其分配给可选属性,最后用所述数据实例化图像。当可选项通过 if-let 子句时,它的 block 执行,抛出错误: Fatal error: Unexpec
swift 5.1 。 考虑以下。 let x: Any? = nil let y: Any = x print("x \(x)") // x nil pri
请耐心听我解释这一点, 我正在创建一个聊天室,用户可以在其中上传照片供其他人查看。当用户点击图标时,他们可以将照片上传到我的 Firebase 数据库(这已成功完成,我已经对此进行了测试)。照片的 U
我的 xCode 5.0 目前遇到一个奇怪的问题:一个对象在控制台中似乎为 nil,但同时它可以通过代码访问。 图 1:对象似乎为零 图2:NSLog(@"%@", imgDownloader) 的输
我有一个实现协议(protocol)的类,以便添加 3 个变量。我专门设置了图像变量,调试器显示该变量存在,但是在我打印它时在代码中显示为 nil,我的 if let 语句也认为该变量为 nil。 @
这个问题在这里已经有了答案: Swift 2 ( executeFetchRequest ) : error handling (5 个答案) 关闭 7 年前。 开始在 SWIFT 中学习编码,每次
两者 (not 'nil) 和 (not nil) 求值为T,那么'nil和nil有什么区别吗?那么 ''nil 呢?如果 ''nil 的计算结果为 'nil,那么 ''nil 是否也应该计算为 ni
(if '(nil nil) 'print-true 'print-false) (if '(nil) 'print-true 'print-false) 在上面的代码
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: obj.nil? vs. obj == nil 我发现了一个问题 - == nil 和 nil 哪个更好?
此代码段抛出异常: x = nil jsoned = x.to_json puts 'x.to_json=' + jsoned.inspect puts 'back=' + JSON.parse(js
基本上我有一个对象想要传递给前端。我在后端记录了它,它不是空的,但是在前端,当我提醒它时,它变成了空。 ... presentation := &presentationStruct { Obje
我创建了一个自定义错误类型来包装错误,以便在 Golang 中更轻松地进行调试。当有错误要打印时它可以工作,但现在它会引起 panic 。 Demo type Error struct { E
写一个符合我当前问题的标题有点难..我有一个 main() 函数,它使用另一个包 (database_sql) 中的函数。该函数初始化一个全局变量 sql.DB*。初始化后,变量不为nil,但是对于其
我是一名优秀的程序员,十分优秀!