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
  • This is not a valid JSON document. Remove the wrapping curly braces and you are good to go. – Darin Dimitrov Commented Jan 31, 2017 at 15:21
  • JSON is not properly formatted, should be this {keyname:[{"name":"Technology"},{"name":"Engineering"},{"name":"Business"}]} – Mohd Asim Suhail Commented Jan 31, 2017 at 15:21
  • The code in the service, where you handle the response, is probably more interesting than your controller. It would be helpful to include it in your question. – henrikmerlander Commented Jan 31, 2017 at 15:35
Add a comment  | 

6 Answers 6

Reset to default 11

The 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