gpt4 book ai didi

json - Swift - 解析数据错误 - Swift.DecodingError.dataCorrupted

转载 作者:行者123 更新时间:2023-12-04 08:46:36 31 4
gpt4 key购买 nike

我将 SwiftSoup 与 Codable 结合使用来获取正确的元素并解析数据。但是,我收到此错误:

JSON decode failed: Swift.DecodingError.dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "No value." UserInfo={NSDebugDescription=No value.})))
我在网上查看,确保我的数据正确匹配,甚至将预期类型从 Int 更改为 String 但得到一个错误,指出 Int 是预期的。
这是我正在接收的数据,可以在控制台中打印:
data - 
{"test":0,"ppace":85,"pshooting":92,"ppassing":91,"pdribbling":95,"pdefending":38,"pphysical":65,"acceleration":91,"sprintspeed":80,"agility":91,"balance":95,"reactions":94,"ballcontrol":96,"dribbling":96,"positioning":93,"finishing":95,"shotpower":86,"longshotsaccuracy":94,"volleys":88,"penalties":75,"interceptions":40,"headingaccuracy":70,"marking":32,"standingtackle":35,"slidingtackle":24,"vision":95,"crossing":85,"freekickaccuracy":94,"shortpassing":91,"longpassing":91,"curve":93,"jumping":68,"stamina":72,"strength":69,"aggression":44,"composure":96}
这是我的功能:
 func parseData() {
do {
let html = try String(contentsOf: url!, encoding: String.Encoding.ascii)
let doc: Document = try! SwiftSoup.parse(html)
let elements = try doc.getAllElements()
for element in elements {
switch element.id() {
case "player_stats_json":

let content = try! element.getElementsContainingText("ppace").text()
print(content.utf8)
let jsonData = content.data(using: .utf8)
do {
self.player = try JSONDecoder().decode(PlayerModel.self, from: Data(jsonData!))
} catch let jsonError as NSError {
print("JSON decode failed: \(jsonError)")
print("data - \(content)")
}
default:
break
}
}

} catch Exception.Error(type: let type, Message: let message) {
print(type)
print(message)
} catch {
print("")
}
}
这是我的模型文件:
struct PlayerModel: Codable {
var test: Int
var ppace: Int
var pshooting: Int
var ppassing: Int
var pdribbling: Int
var pdefending: Int
var pphysical: Int
var acceleration: Int
var sprintspeed: Int
var agility: Int
var balance: Int
var reactions: Int
var ballcontrol: Int
var dribbling: Int
var positioning: Int
var finishing: Int
var shotpower: Int
var longshotsaccuracy: Int
var volleys: Int
var penalties: Int
var interceptions: Int
var headingaccuracy: Int
var marking: Int
var standingtackle: Int
var slidingtackle: Int
var vision: Int
var crossing: Int
var freekickaccuracy: Int
var shortpassing: Int
var longpassing: Int
var curve: Int
var jumping: Int
var stamina: Int
var strength: Int
var aggression: Int
var composure: Int
}
我已经两次和三次检查变量名称与每个键匹配。我已将控制台中输出的数据粘贴到在线 JSON 验证器中,并且没有出现错误。我现在正挠头,因为我不明白我哪里出错了。

最佳答案

我仍然不知道你的解析有什么问题。我已设法解析您的 html,但我不知道为什么在尝试使用 Int 而不是 Uint8 时,它始终无法返回正确的值。这对我有用:

import UIKit
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
struct PlayerModel: Codable {
let test: UInt8
let ppace: UInt8
let pshooting: UInt8
let ppassing: UInt8
let pdribbling: UInt8
let pdefending: UInt8
let pphysical: UInt8
let acceleration: UInt8
let sprintspeed: UInt8
let agility: UInt8
let balance: UInt8
let reactions: UInt8
let ballcontrol: UInt8
let dribbling: UInt8
let positioning: UInt8
let finishing: UInt8
let shotpower: UInt8
let longshotsaccuracy: UInt8
let volleys: UInt8
let penalties: UInt8
let interceptions: UInt8
let headingaccuracy: UInt8
let marking: UInt8
let standingtackle: UInt8
let slidingtackle: UInt8
let vision: UInt8
let crossing: UInt8
let freekickaccuracy: UInt8
let shortpassing: UInt8
let longpassing: UInt8
let curve: UInt8
let jumping: UInt8
let stamina: UInt8
let strength: UInt8
let aggression: UInt8
let composure: UInt8
}
let url = URL(string:"https://www.futbin.com/21/player/541/lionel-messi")!
URLSession.shared.dataTask(with: url) { data, response, error in
guard let data = data, let html = String(data: data, encoding: .utf8) else { return }
if let start = html.range(of: #"<div style="display: none;" id="player_stats_json">"#)?.upperBound,
let end = html[start...].range(of: #"</div>"#)?.lowerBound {
let json = html[start..<end]
do {
let player = try JSONDecoder().decode(PlayerModel.self, from: Data(json.utf8))
print(player)
} catch {
print(error)
}
}
}.resume()

这将打印

PlayerModel(test: 0, ppace: 85, pshooting: 92, ppassing: 91,pdribbling: 95, pdefending: 38, pphysical: 65, acceleration: 91,sprintspeed: 80, agility: 91, balance: 95, reactions: 94, ballcontrol:96, dribbling: 96, positioning: 93, finishing: 95, shotpower: 86,longshotsaccuracy: 94, volleys: 88, penalties: 75, interceptions: 40,headingaccuracy: 70, marking: 32, standingtackle: 35, slidingtackle:24, vision: 95, crossing: 85, freekickaccuracy: 94, shortpassing: 91,longpassing: 91, curve: 93, jumping: 68, stamina: 72, strength: 69,aggression: 44, composure: 96)

关于json - Swift - 解析数据错误 - Swift.DecodingError.dataCorrupted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64286377/

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