AngularJS: unable to parse JSON list

The following is the JSON I'm trying to parse:

{[{"name":"Technology"},{"name":"Engineering"},{"name":"Business"}]} 

I get an error message below:

 Unexpected token [ in JSON at position 1 at JSON.parse (<anonymous>) at fromJson 

I get data from the service and save it in the controller area using the following:

 vm = this; vm.sectorList = response.data; 

And my HTML:

 <div ng-repeat="sector in ctrl.sectorList"> {{sector.name}} </div> 
+5
source share
6 answers

You must remove the curly braces before [] in your JSON:

 [{"name":"Technology"},{"name":"Engineering"},{"name":"Business"}] 
+3
source

JSON is invalid, hence the error you are getting. You are missing the key to use the value of the JSON array. This, for example, (the "nothing" key is added) will work:

 {"anything": [{"name":"Technology"},{"name":"Engineering"},{"name":"Business"}]} 
+11
source

If the answer is an array, it should not be enclosed in braces {} . That would be true JSON:

 [{"name":"Technology"}, {"name":"Engineering"}, {"name":"Business"}] 

You are also trying to use JSON.parse for an object when it takes a string as an input parameter. If you want to make an object into a JSON string, you must use JSON.stringify .

+6
source

Your JSON invalid, you can confirm your JSON here

It should be:

 [{ "name": "Technology" }, { "name": "Engineering" }, { "name": "Business" }] 
+5
source

Your key is missing.

 {"YourKEY": [{"name":"Technology"},{"name":"Engineering"},{"name":"Business"}]} 

You cannot access an array object without a key.

+3
source

you need to assign an array to an object property. cannot assign an array only inside an object without a property

 { "items":[ { "name":"Technology" }, { "name":"Engineering" }, { "name":"Business" } ] } 

in ng-repeat it should change as follows

 <div ng-repeat="sector in ctrl.sectorList.items"> {{sector.name}} </div> 
+3
source

Source: https://habr.com/ru/post/1263623/


All Articles