新手Json
在调用json我的json文件时遇到此错误',' or ']'
[
{
"modules":
[
{
"title":"name of module1",
"description":"description of module1",
"weeks":[{"id":1, "title":"Week 01"}]
},
{
"title":"name of module2",
"description":"description of module2",
"weeks":[{"id":2, "title":"Week 02"},{"id":3,"title":"Week 03"}]
}
]
},
{
"products":
[
{
"url":"http://dummyimage.com/242x200/f3f3f3/2d2d2d.png",
"description":"Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.",
"viewurl" : "/",
"savebtn" : "save"
},
{
"url":"http://dummyimage.com/242x200/f3f3f3/2d2d2d.png",
"description":"Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.",
"viewurl" : "/",
"savebtn" : "save"
}
]
}
]
希望我做得正确我不知道它正在尝试在角度js控制器中使用Error: JSON.parse: expected ',' or ']' after array element
正在发生什么
app.controller('settingsController', function($scope, $http){
$http.get('assets/javascripts/datas.json').then(function(result){
$scope.employe = result.data;
$scope.prod = result
})
});
并在视野中
<div class="col-sm-6 col-md-4" ng-repeat="datas in prod.products">
<div class="thumbnail">
<img ng-src="{{ datas.url }}" alt="product">
<div class="caption">
<h3>{{ datas.caption}} </h3>
<p>{{ datas.description}}</p>
<p><a role="button" class="btn btn-primary" ng-href="{{datas.viewurl}}">Button</a> <a role="button" class="btn btn-default" href="#">{{datas.savebtn}}</a></p>
</div>
</div>
</div>
和控制台中的错误
错误:JSON.parse:预期&#39;,&#39;或者&#39;]&#39;在JSON数据的第76行第9行的数组元素之后cc@/test/assets/javascripts/vendors/angular.js:14:289 Ud / this.defaults.transformResponse&lt; @ / test / assets / javascripts / vendors / angular。 js:69:58 xc /&lt; @ / test / assets / javascripts / vendors / angular.js:68:352 r@/test/assets/javascripts/vendors/angular.js:7:28 xc @ / test / assets /javascripts/vendors/angular.js:68:336 b@/test/assets/javascripts/vendors/angular.js:70:65 ye /e/l.promise.then/K@/test/assets/javascripts/vendors /angular.js:100:59 ye / f /&lt; .then /&lt; @ / test / assets / javascripts / vendors / angular.js:101:235 Zd / this。$ get
答案 0 :(得分:1)
当我从字符串文字解析JSON时,我发生了这个错误。当我复制JSON时,我忘记了转义序列需要双重转义。
问题示例:
var data = JSON.parse('[{"key": "This value has \"a quoted\" string"}]');
虽然字符串值是有效的JSON,但转义序列会丢失。然后需要双重逃脱:
更正版本:
var data = JSON.parse('[{"key": "This value has \\\"a quoted\\\" string"}]');
答案 1 :(得分:0)
对我来说,如果我在JSON搅拌中有一个://(例如http://),就会发生问题,因为我替换了它,所以它起作用了。