Angular不解析嵌套的JSON数组

时间:2015-04-30 10:31:13

标签: arrays json angularjs parsing nested

在我的控制器中,我执行http GET请求来获取JSON数组。 但是,在我能够对此JSON数组执行任何操作之前,Angular会提供解析错误。

JSON数组:

[
  [
    "",
    "postcond",
    "Stéphanie:stap 1",
    "Actor1:stap 2",
    "Stéphanie:stap 3"
  ],
  [
    "",
    "postcond",
    "Stéphanie:stap 1",
    "Actor1:stap 2",
    "Stéphanie:stap 1"
  ]
]

http请求:

$http.get(serverPrefix + "project/" + projectid + "/usecase/" + usecaseid+"/stories")
.success(function (response) {


})
.error(function (response) {$location.path('/project/'+projectid+'/usecase/'+usecaseid); });

错误:

SyntaxError: Unexpected token [
at Object.parse (native)
at pc (http://vopro5.ugent.be/stephanie/scripts/angular.min.js:14:208)
at Zb (http://vopro5.ugent.be/stephanie/scripts/angular.min.js:76:379)
at http://vopro5.ugent.be/stephanie/scripts/angular.min.js:77:237
at s (http://vopro5.ugent.be/stephanie/scripts/angular.min.js:7:302)
at Zc (http://vopro5.ugent.be/stephanie/scripts/angular.min.js:77:219)
at c (http://vopro5.ugent.be/stephanie/scripts/angular.min.js:78:349)
at http://vopro5.ugent.be/stephanie/scripts/angular.min.js:112:20
at l.$get.l.$eval (http://vopro5.ugent.be/stephanie/scripts/angular.min.js:125:305)
at l.$get.l.$digest (http://vopro5.ugent.be/stephanie/scripts/angular.min.js:122:398)

根据http://jsonlint.com/,它是一个有效的JSON字符串。

1 个答案:

答案 0 :(得分:0)

为范围对象分配值,您将获得数组0和1索引值。尝试使用以下代码,让我......

  $scope.test = [
[
    "",
    "postcond",
    "Stéphanie:stap 1",
    "Actor1:stap 2",
    "Stéphanie:stap 3"
],
[
    "",
    "postcond",
    "Stéphanie:stap 1",
    "Actor1:stap 2",
    "Stéphanie:stap 1"
]
];

for (var key in $scope.test) {
    if ($scope.test.hasOwnProperty(key)) {
        console.log(key + "-- " + $scope.test[key]);
    }
}