I have this json I want to access the elements of this.
{
"status" : true,
"code" : 200,
"results" : [
{
"choice" : null,
"assessment" : 133,
"id" : 863,
"title" : "What do you like to eat duing a movie?",
"choices" : [
{
"id" : 1738,
"title" : "Popcorn",
"nas_class" : "c1",
"question" : 863,
"questions" : [
{
"choice" : 1738,
"assessment" : 133,
"id" : 864,
"title" : "What flavours of popcorn do you like?",
"choices" : [
{
"id" : 1739,
"title" : "Caramel",
"nas_class" : "c1",
"question" : 864,
"questions" : [
]
},
{
"id" : 1740,
"title" : "Cheese",
"nas_class" : "c1",
"question" : 864,
"questions" : [
]
},
{
"id" : 1741,
"title" : "Tomato",
"nas_class" : "c1",
"question" : 864,
"questions" : [
]
},
{
"id" : 1742,
"title" : "Chilli",
"nas_class" : "c1",
"question" : 864,
"questions" : [
{
"choice" : 1742,
"assessment" : 133,
"id" : 865,
"title" : "How good is your spice tolerance?",
"choices" : [
],
"description" : "",
"type" : "rating",
"creator" : 5
}
]
}
],
"description" : "",
"type" : "multi_select",
"creator" : 5
}
]
},
{
"id" : 1743,
"title" : "Nachos",
"nas_class" : "c1",
"question" : 863,
"questions" : [
]
},
{
"id" : 1744,
"title" : "Chips",
"nas_class" : "c1",
"question" : 863,
"questions" : [
]
}
],
"description" : "",
"type" : "single_select",
"creator" : 5
},
{
"choice" : null,
"assessment" : 133,
"id" : 866,
"title" : "Describe the kind of stories you enjoy watching",
"choices" : [
],
"description" : "",
"type" : "free_text",
"creator" : 5
},
{
"choice" : null,
"assessment" : 133,
"id" : 867,
"title" : "Sign here to indicate that you consent selling of your data",
"choices" : [
],
"description" : "",
"type" : "signature",
"creator" : 5
},
{
"choice" : null,
"assessment" : 133,
"id" : 868,
"title" : "Upload a picture of your driving license",
"choices" : [
],
"description" : "",
"type" : "file_upload",
"creator" : 5
},
{
"choice" : null,
"assessment" : 133,
"id" : 869,
"title" : "Give your opinion",
"choices" : [
{
"id" : 1745,
"title" : "Bad",
"nas_class" : "c1",
"question" : 869,
"questions" : [
]
},
{
"id" : 1746,
"title" : "Poor",
"nas_class" : "c1",
"question" : 869,
"questions" : [
]
},
{
"id" : 1747,
"title" : "Good",
"nas_class" : "c1",
"question" : 869,
"questions" : [
]
},
{
"id" : 1748,
"title" : "Excellent",
"nas_class" : "c1",
"question" : 869,
"questions" : [
]
}
],
"description" : "",
"type" : "single_select",
"creator" : 5
}
],
"msg" : "Success"
}
I am able to access the first array at index 0, and can reach the end of the nested array but need to shift to the index 1 and so onn after I reach to the end of the n array elements how do I make it happen,
{
"choice" : null,
"assessment" : 133,
"id" : 863,
"title" : "What do you like to eat duing a movie?",
"choices" : [
{
"id" : 1738,
"title" : "Popcorn",
"nas_class" : "c1",
"question" : 863,
"questions" : [
{
"choice" : 1738,
"assessment" : 133,
"id" : 864,
"title" : "What flavours of popcorn do you like?",
"choices" : [
{
"id" : 1739,
"title" : "Caramel",
"nas_class" : "c1",
"question" : 864,
"questions" : [
]
},
{
"id" : 1740,
"title" : "Cheese",
"nas_class" : "c1",
"question" : 864,
"questions" : [
]
},
{
"id" : 1741,
"title" : "Tomato",
"nas_class" : "c1",
"question" : 864,
"questions" : [
]
},
{
"id" : 1742,
"title" : "Chilli",
"nas_class" : "c1",
"question" : 864,
"questions" : [
{
"choice" : 1742,
"assessment" : 133,
"id" : 865,
"title" : "How good is your spice tolerance?",
"choices" : [
],
"description" : "",
"type" : "rating",
"creator" : 5
}
]
}
],
"description" : "",
"type" : "multi_select",
"creator" : 5
}
]
},
{
"id" : 1743,
"title" : "Nachos",
"nas_class" : "c1",
"question" : 863,
"questions" : [
]
},
{
"id" : 1744,
"title" : "Chips",
"nas_class" : "c1",
"question" : 863,
"questions" : [
]
}
],
"description" : "",
"type" : "single_select",
"creator" : 5
}
using this functions
for converting the n array tree into the nested array,
I am new to the nested array , any help will be appreciated.
self.treeArray = self.convertTreeToArray(assesmentDataValue)
func convertTreeToArray(_ roots: [ResultsQuestions]?) -> [TreeItem] {
guard let roots = roots else {
return []
}
var result: [TreeItem] = []
for root in roots {
result.append(contentsOf: convertNodeToArray(root))
}
// print(result)
return result
}
func convertNodeToArray(_ node: ResultsQuestions) -> [TreeItem] {
var result: [TreeItem] = [TreeItem(id: node.id,
title: node.title,
description: node.description,
type: node.type,
assessment: node.assessment,
choices: convertChoices(node.choices),
choice: node.choice,
creator: node.creator ?? 0,
isSelected: nil)]
if let choices = node.choices {
for choice in choices {
if let questions = choice.questions {
result += questions.flatMap { convertNodeToArray($0) }
}
}
}
return result
}
func convertChoices(_ choices: [Choice]?) -> [ChoiceItem]? {
guard let choices = choices else {
return nil
}
return choices.map { choice in
return ChoiceItem(id: choice.id,
title: choice.title,
nasClass: choice.nasClass,
question: choice.question,
questions: choice.questions?.flatMap { convertNodeToArray($0) })
}
}
How can I go to the next index of the array ,