gpt4 book ai didi

ios - 如何解析嵌套的 JSON。我得到 nil 值作为输出

转载 作者:行者123 更新时间:2023-11-28 23:21:53 26 4
gpt4 key购买 nike

这些是我创建的结构

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
}
]
}
]

最佳答案

  1. let catid : String? 应该是 let catid : Int?
  2. 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com