如何读取角度js中的.json文件

时间:2014-08-18 06:28:10

标签: ajax json angularjs

这里我想读一个.json文件。我必须在控制器中阅读它。但我在阅读.json文件时得到了。 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"
                          }
                       ]
                    },

}
}
]

我试着看到我得到了意外的令牌/。任何人都可以建议如何阅读它?

2 个答案:

答案 0 :(得分:1)

您发布的JSON不正确。

这是需要的格式:

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

和我用来以角度遍历JSON的html:

<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

答案 1 :(得分:0)

您可以在此处粘贴JSON结构 - http://jsonformatter.curiousconcept.com/

粘贴后,您会看到JSON在其结构中存在一些错误。

正确的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"
                          }
                       ]
                    }
                 ]
              }
           ]
        }
     }
   }
]