admin管理员组文章数量:1202338
The following is the JSON I'm trying to parse:
{[{"name":"Technology"},{"name":"Engineering"},{"name":"Business"}]}
I'm getting the error below:
Unexpected token [ in JSON at position 1
at JSON.parse (<anonymous>)
at fromJson
I'm getting the data from a service and saving it into the controller scope using the following:
vm = this;
vm.sectorList = response.data;
And my HTML:
<div ng-repeat="sector in ctrl.sectorList">
{{sector.name}}
</div>
The following is the JSON I'm trying to parse:
{[{"name":"Technology"},{"name":"Engineering"},{"name":"Business"}]}
I'm getting the error below:
Unexpected token [ in JSON at position 1
at JSON.parse (<anonymous>)
at fromJson
I'm getting the data from a service and saving it into the controller scope using the following:
vm = this;
vm.sectorList = response.data;
And my HTML:
<div ng-repeat="sector in ctrl.sectorList">
{{sector.name}}
</div>
Share
Improve this question
asked Jan 31, 2017 at 15:17
kulsoompatelkulsoompatel
2301 gold badge3 silver badges18 bronze badges
3
|
6 Answers
Reset to default 11The JSON is invalid, hence the error you're getting. You're missing a key to go with the JSON array value. This for instance (key "anything" added) will work:
{"anything": [{"name":"Technology"},{"name":"Engineering"},{"name":"Business"}]}
If the response is an array it should not be enclosed in curly brackets {}
. This would be valid JSON:
[{"name":"Technology"}, {"name":"Engineering"}, {"name":"Business"}]
You are also trying to use JSON.parse
on an object, when it takes a string as input parameter. If you want to make an object into a JSON string you should use JSON.stringify
.
Your JSON
is invalid you can validate your JSON
Here
It should be:
[{
"name": "Technology"
}, {
"name": "Engineering"
}, {
"name": "Business"
}]
You should remove the curly braces before the []
in your JSON :
[{"name":"Technology"},{"name":"Engineering"},{"name":"Business"}]
Your key is missing.
{"YourKEY": [{"name":"Technology"},{"name":"Engineering"},{"name":"Business"}]}
You cannot access object of arrays without key.
you need to assign the array to property of the object. can't assign array just inside of the object without property
{
"items":[
{
"name":"Technology"
},
{
"name":"Engineering"
},
{
"name":"Business"
}
]
}
in the ng-repeat it should change as following
<div ng-repeat="sector in ctrl.sectorList.items">
{{sector.name}}
</div>
本文标签: javascriptAngularJS Unable to parse a list of JSONStack Overflow
版权声明:本文标题:javascript - AngularJS: Unable to parse a list of JSON - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738563714a2099731.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
{keyname:[{"name":"Technology"},{"name":"Engineering"},{"name":"Business"}]}
– Mohd Asim Suhail Commented Jan 31, 2017 at 15:21