Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Is there a better way, or the correct one to have this approach for a JSON object that hast the following structure? (it's only a portion of my object, the most important one)

{
    "Questions": [
        {
            "AnswerType": "single-choice",
            "Answers": [
                {
                    "AnswerValue": null,
                    "Description": "Yes",
                    "ID": 124877,
                    "Questions": [
                        {
                            "AnswerType": "multiline",
                            "Answers": [
                                {
                                    "AnswerValue": null,
                                    "Description": null,
                                    "ID": 124879,
                                    "Questions": []
                                }
                            ],
                            "Description": "Other text",
                            "ID": 37932,
                            "SelectedAnswers": []
                        },
                        {
                            "AnswerType": "singleline",
                            "Answers": [
                                {
                                    "AnswerValue": null,
                                    "Description": null,
                                    "ID": 124880,
                                    "Questions": []
                                }
                            ],
                            "Description": "Hoeveelperweek?",
                            "ID": 37933,
                            "SelectedAnswers": []
                        }
                    ]
                },
                {
                    "AnswerValue": null,
                    "Description": "No",
                    "ID": 124878,
                    "Questions": []
                }
            ],
            "Description": "Question text",
            "ID": 37931,
            "SelectedAnswers": []
        }
    ]
}

AngularJS template for parsing it recursively:

<div ng-repeat="question in myData.Questions">
    {{question.Description}}
    <div ng-repeat="answer in myData.Questions[$index].Answers">
        <a href="#" ng-click="answerQuestion(answer)">{{answer.Description}}</a>
        <div ng-repeat="qanswer in myData.Questions[$index].Answers[$index].Questions">
            <a href="#" ng-click="answerQuestion(qanswer)">{{qanswer.Description}}</a>
        </div>
    </div>
</div>

The structure can go infinitely. This is the structure that is being repeated over and over:

Questions []
-Answers []
--Answer [i]
---Questions []

JSBin example is here

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.