0

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?

0

2 Answers 2

1

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

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

5 Comments

Thanks. Could you suggest how to show each question at a time?
I would suggest to change the structure of your json as it is deeply nested and there are many ng-repeats (which means many $digests) so avoid a nested ng-repeat....
This is my requirement so i have to follow this
oh ok then you can use bindonce in order to reduce the number of $watchers.
I can able to get only last question options.Can you explain how can i show each question with respect of its options?@V31
0

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"
                          }
                       ]
                    }
                 ]
              }
           ]
        }
     }
   }
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.