这是我试图解析的JSON的结构。我希望能够循环访问问题。
{
"status" : 200,
"C&Q" : [
{
"category" : "Applicant Details",
"questions" : [
{
"compulsory" : true,
"options" : "Mr.; Mrs.; Miss",
"input" : "radio",
"question" : "Title"
},
{
"compulsory" : true,
"options" : "N\/A",
"input" : "text",
"question" : "Surname"
}
]
},
{
"category" : "Identification Documents",
"questions" : [
{
"compulsory" : true,
"options" : "Passport;Drivers License;Voter ID;NHIS",
"input" : "radio",
"question" : "ID TYPE"
},
{
"compulsory" : true,
"options" : "N\/A",
"input" : "date",
"question" : "Date of Issue (YYYY-MM-DD)"
}
]
}
此处的代码,但我只能通过索引访问。也许有人可以提供有关如何循环的示例代码,以便我可以访问问题数组。
let swiftyJsonVar = JSON(response.result.value!)
guard let questions = swiftyJsonVar["C&Q"].arrayValue[0]["questions"].arrayObject as? [[String:AnyObject]] else
{
print("parsing failed")
return
}
这就是我使用/计划使用适合上述解析的数组的方法。
let questionsStructs = questions.map(Questions.init)
self.formsData = questionsStructs
任何帮助将不胜感激。
答案 0 :(得分:0)
var questions:[Question] = []
for (key,subJson):(String, JSON) in json["C&Q"] {
for (key,j):(String, JSON) in subJson["questions"] {
let question = Question(......)
questions.append(question)
}
}