Anusha Nilapu
Anusha Nilapu

Reputation: 1303

How read .json file in angular js

Here i want to read a .json file. I have to read it in controller. But i am getting while reading the .json file. quiz.json:

 [
 {
   "data":{
  "questions":{

           "level":[
              {
                 "question":[
                    {
                       "title":"What does SAP® stand for?",
                       "answer":[
                          "Services for Application Programming",
                          {
                             "_correct":"1",
                             "__text":"Systems, Applications, and Programs"
                          },
                          "Sino-American Peace",
                          "Statistical Analysis Program"
                       ]
                    },
                    {
                       "title":"What does Tcode mean?",
                       "answer":[
                          "Television Code",
                          "Translation Code",
                          "Transition Code",
                          {
                             "_correct":"1",
                             "__text":"Transaction Code"
                          }
                       ]
                    },

}
}
]

I tried to read i got Unexpected token /. Can anyone suggest how to read it?

Upvotes: 0

Views: 125

Answers (2)

Egor Smirnov
Egor Smirnov

Reputation: 603

You could paste your JSON structure here - http://jsonformatter.curiousconcept.com/

After pasting you will see that JSON has some errors in its structure.

Correct JSON will be:

[
 {
   "data":{
  "questions":{

           "level":[
              {
                 "question":[
                    {
                       "title":"What does SAP® stand for?",
                       "answer":[
                          "Services for Application Programming",
                          {
                             "_correct":"1",
                             "__text":"Systems, Applications, and Programs"
                          },
                          "Sino-American Peace",
                          "Statistical Analysis Program"
                       ]
                    },
                    {
                       "title":"What does Tcode mean?",
                       "answer":[
                          "Television Code",
                          "Translation Code",
                          "Transition Code",
                          {
                             "_correct":"1",
                             "__text":"Transaction Code"
                          }
                       ]
                    }
                 ]
              }
           ]
        }
     }
   }
]

Upvotes: 0

V31
V31

Reputation: 7666

The JSON you posted was incorrect.

This is the format that needs to be their:

JSON:

$scope.questions = [
 {
   "data":{
  "questions":{

           "level":[
              {
                 "question":[
                    {
                       "title":"What does SAP® stand for?",
                       "answer":[
                          "Services for Application Programming",
                          {
                             "_correct":"1",
                             "__text":"Systems, Applications, and Programs"
                          },
                          "Sino-American Peace",
                          "Statistical Analysis Program"
                       ]
                    },
                    {
                       "title":"What does Tcode mean?",
                       "answer":[
                          "Television Code",
                          "Translation Code",
                          "Transition Code",
                          {
                             "_correct":"1",
                             "__text":"Transaction Code"
                          }
                       ]
                    }
                 ]
               }
             ]
           }
         }
       }
     ];

and the html that I used to traverse the JSON in angular:

<div ng-app>
    <div ng-controller = "test">
        <div ng-repeat="data1 in questions">
            <div ng-repeat="question in data1.data.questions.level">
                    <div ng-repeat="levelQuest in question.question">
                        {{levelQuest.title}}
                </div>
            </div>
        </div>
    </div>
</div>

Working Demo

Upvotes: 1

Related Questions