gpt4 book ai didi

json - 使用 Freddy SDK 解析 JSON 文件

转载 作者:行者123 更新时间:2023-11-30 12:40:41 26 4
gpt4 key购买 nike

使用 Freddy SDK 解析 JSON 文件时,我很难以正确的格式获取一些值。 ,我正在尝试使用 JSON 解析测验中的一些问题和答案,如下所示:

  {
"success":true,
"quiz":[
{
"question":"Which of the following is required by law?",
"answers":[
["To have your dog microchipped and correct details.", true],
["To have your dog wear a collar and tag with correct contact details.", true],
["To register your dog with the local Dog Warden.", false],
["To pick up after your dog.", true]
],
"imageFileName":"ChippedDog.jpg"
}
]
}

我遇到的麻烦是获取答案及其 Bool 值。我的解析代码是:

func createQuestionObjectsFromJSON() -> [Question] {

self.questions = []
let filePath = Bundle.main.path(forResource: self.fileName, ofType: self.fileType)
if let data = NSData(contentsOfFile:filePath!) {
do {
let json = try JSON(data: data as Data)
for questionJSON in try json.getArray(at:"quiz") {

var question = Question()
question.text = try questionJSON.getString(at: "question")
question.answers = try questionJSON.getArray(at: "answers") as [AnyObject]
question.imageFileName = try questionJSON.getString(at: "imageFileName")
self.questions?.append(question)
}
} catch {
print("Error parsing questions part of JSON file")
}
}

return questions

}

但是当我尝试访问 cellForRow 中的每个答案时,我却无法访问。

如果调用

让答案= currentQuestion.answers[indexPath.row]

然后打印它,我得到

[给你的狗植入微芯片并正确详细信息。,正确]

但第一个值不是应有的字符串,如果我调用

答案[0]

我得到零。我有点迷失,确信有一个简单的解决办法,但我就是看不到它。

任何帮助都会很棒。

更新:问题结构:

导入UIKit

struct Question {

var text: String!
var answers: [AnyObject]!
var imageFileName: String!
}

最佳答案

尝试 Question.answers = 尝试 QuestionJSON.getArray(at: "answers") as [Array]


而不是: Question.answers = 尝试 QuestionJSON.getArray(at: "answers") as [AnyObject]

关于json - 使用 Freddy SDK 解析 JSON 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42262700/

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