3

I have a JSON file and I want to display it in my HTML. But I dont know how to use the syntax for deeply nested objects.

  {
  "questions": [
    {
      "question": "Qw1",
      "answers": [
        {
          "answers": "An1",
          "value": 25
        },
        {
          "answers": "An2",
          "value": 50
        },
        {
          "answers": "An3",
          "value": 75
        },
        {
          "answers": "An4",
          "value": 100
        }
      ]
    },
    {
      "question": "Qw2",
      "answers": [
        {
          "answers": "An1",
          "value": 25
        },
        {
          "answers": "An2",
          "value": 50
        },
        {
          "answers": "An3",
          "value": 75
        },
        {
          "answers": "An4",
          "value": 100
        }
      ]
    }
  ]
}

I am not trying to use ng-repeat, as I need to access them them one by one.

 <div class="main-content" ng-controller="QuestionCtrl">
                <div id="Content1">
                  <h1>Question 1</h1>
                  <p>{{questions.question[1].answer}}</p>
                  ...........

Ofcourse it doesn't work. How do I acces my information?

3 Answers 3

7

There is a working plunker

Using this controller:

.controller('QuestionCtrl', ['$scope', '$http', function ($scope, $http) { 
  $scope.questions = [];
  $http
    .get("data.json")
    .then(function(response){
      $scope.questions = response.data.questions;
    });
}]) 

which is loading your data, we can use this

<div class="main-content" ng-controller="QuestionCtrl">
  <div id="Content1">
  <h1>Question 1</h1>
  <p>{{questions[0].question}}</p>
  <p>{{questions[0].answers[0]}}</p>
  <p>{{questions[0].answers[1]}}</p>
  </div>
</div>

Or we can use it like this:

<h1>Question 1</h1>
<p>{{questions[0].question}}</p>
<h2>answer 1</h2>
<p>{{questions[0].answers[0].value}}</p>
<p>{{questions[0].answers[0].answers}}</p>
<h2>answer 2</h2>
<p>{{questions[0].answers[1].value}}</p>
<p>{{questions[0].answers[1].answers}}</p>

which will show the numeric value and answers text

Check it here in action

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the effort. But instead of {"answers":"An1","value":25} how can I fetch just their value?
I updated the plunker (reopen that plunker) and also the answer. that should show you how to. We can simply access the answers[0].answers to get text and answer[0].value to get the number... good luck
Tnx! I know this has been an easy one xD
3
<div class="main-content" *ngFor="let q of questions">
   <h1> {{q.question}}</h1>
    <p *ngFor="let ans of q.answers">{{ans.answers}} = {{ans.value}} </p>
</div>

Comments

1

questions is the array, not questions.question, and your JSON does not contain answer properties. Did you try using the correct traversal?

questions[0].answers[0].value

should get you 25.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.