我正在尝试使用Swift中的新(ish)可编码功能从JSON文件传递一些数据。我之前使用过以下语法没有问题。我相信我可能有一些设置错误,但是,我似乎无法理解为什么当JSON格式被JSON解析器批准时我会继续收到以下消息。
错误消息:
错误:dataCorrupted(swift.DecodingError.Context(codingPath:[],debugDescription:&#34;给定数据无效JSON。&#34;,underlyingError:可选(错误域= NSCocoaErrorDomain代码= 3840&# 34; JSON文本不以数组或对象和选项开头,以允许未设置片段。&#34; UserInfo = {NSDebugDescription = JSON文本不以数组或对象开头,并且选项允许未设置片段。}))< / p>
我的QuestionFactory文件中的代码......
class QuestionFactory {
func parseJSON(filename fileName: String) -> Quiz? {
if let url = Bundle.main.url(forResource: fileName, withExtension: "json") {
print(url)
do {
let data = try Data(contentsOf: url)
let decoder = JSONDecoder()
print("data received is \(data.count) bytes:\n\(data)")
print(data)
print(data as NSData)
let jsonData = try decoder.decode(Quiz.self, from: data)
print(jsonData)
} catch {
print("error:\(error)")
}
}
return nil
}
}
我最初的ViewController中的代码:
class LaunchScreen: UIViewController {
private var quiz: Quiz?
private let jsonFileName = "QuizData"
func viewDidLoad() {
super.viewDidLoad()
createQuiz()
}
private func createQuiz() {
let questionFactory = QuestionFactory()
guard let parsedQuiz = questionFactory.parseJSON(filename: jsonFileName) else {
print("Error creating quiz")
return
}
quiz = parsedQuiz
}
func movesToMainMenuScreen() {
let transition = CATransition()
transition.duration = 1.5
transition.type = kCATransitionFade
self.navigationController?.view.layer.add(transition, forKey:nil)
let mainMenuVC: UIViewController = MainMenuViewController(quiz: quiz!) >> I am receiving an error here as well, perhaps due to my mainMenuVC's required init?
navigationController?.pushViewController(mainMenuVC, animated: false)
}
在我的mainMenuViewController中:
class mainMenuViewController: UIViewController {
private var quiz: Quiz! {
didSet {
tableViewAdapter = AnswerTableViewAdapter(answers: quiz.questions[0].answers) >> Although, it is not obviously reaching this far to read through the JSON.
}
required init(quiz: Quiz) {
super.init(nibName: nil, bundle: nil)
defer {
self.quiz = quiz
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
JSON看起来像这样......
{
"questions":[
{
"text": "1. Where will the 2022 World cup be held?",
"answers": [
{
"text": "Qatar",
"isCorrect": true,
"answerType": "2"
},
{
"text": "دولة قطر",
"isCorrect": true,
"answerType": "1"
},
{
"text": "Jamaica",
"isCorrect": false,
"answerType": "0"
},
{
"image":"qatarFlag",
"isCorrect": true,
"answerType": "3"
}
]
}]
}
模型文件....
Quiz.swift
import Foundation
struct Quiz: Decodable {
var questions: [Question]
}
Question.swift
import Foundation
struct Question: Decodable {
var text: String
var answers: [Answer]
}
Answer.swift
import Foundation
struct Answer: Decodable {
var text: String
var image: String
var isCorrect: Bool
var answerType: String
}
答案 0 :(得分:0)
还有额外的
]
}
在最后两行添加删除这两个右括号并尝试解析JSON。
并将模型的属性设置为可选,以避免可以为空的崩溃。
模型文件....
Quiz.swift
import Foundation
struct Quiz: Decodable {
var questions: [Question]?
}
Question.swift
import Foundation
struct Question: Decodable {
var text: String?
var answers: [Answer]?
}
Answer.swift
import Foundation
struct Answer: Decodable {
var text: String?
var image: String?
var isCorrect: Bool?
var answerType: String?
}