Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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?

share|improve this question
up vote 2 down vote accepted

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

share|improve this answer
    
Thanks for the effort. But instead of {"answers":"An1","value":25} how can I fetch just their value? – CountGradsky Jan 27 '15 at 18:26
    
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 – Radim Köhler Jan 27 '15 at 18:31
    
Tnx! I know this has been an easy one xD – CountGradsky Jan 27 '15 at 18:39

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.

share|improve this answer

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.