stygma
stygma

Reputation: 523

Unexpected token { when parsing 'valid' json

I am trying to assign an array stored in json into an array in a controller when I press a button. I have run my json against several online validators and all of them claim my json is valid. However, when I go to parse it I get an unexpected token error.

I have double checked everything and I think everything closely mirrors the tutorial I am going through at https://docs.angularjs.org/tutorial/step_05 but it just doesn't want to work.

Relevant Controller Lines:

angular.module('myApp.controllers', [])
  .controller('PlayerAdditionCtrl', function($scope, $http) {
        $scope.players = [];
        $scope.loadDefaults = function()
        {
            $http.get('defaults.json').success(function(array)
            {
                        //assignment here
            });
        }
  });

Complete json:

[
    { 
        "name": "Mike",
        "color": "teal"
    },
    {
        "name": "Madre",
        "color": "seagreen"
    },
    {
        "name": "Anthony",
        "color": "royalblue"
    },
    {
        "name": "GI-Joe",
        "color": "olivedrab"
    }
]

Complete error text from chrome:

    SyntaxError: Unexpected token {
        at Object.parse (native)
        at fromJson (http://localhost:8000/app/bower_components/angular/angular.js:1078:14)
        at $HttpProvider.defaults.defaults.transformResponse (http://localhost:8000/app/bower_components/angular/angular.js:7317:18)
        at http://localhost:8000/app/bower_components/angular/angular.js:7292:12
        at Array.forEach (native)
        at forEach (http://localhost:8000/app/bower_components/angular/angular.js:323:11)
        at transformData (http://localhost:8000/app/bower_components/angular/angular.js:7291:3)
        at transformResponse (http://localhost:8000/app/bower_components/angular/angular.js:7963:17)
        at wrappedCallback (http://localhost:8000/app/bower_components/angular/angular.js:11319:81)
        at http://localhost:8000/app/bower_components/angular/angular.js:11405:26 angular.js:9778
    (anonymous function) angular.js:9778
    (anonymous function) angular.js:7216
    wrappedCallback angular.js:11322
    (anonymous function) angular.js:11405
    Scope.$eval angular.js:12412
    Scope.$digest angular.js:12224
    Scope.$apply angular.js:12516
    done angular.js:8204
    completeRequest angular.js:8412
    xhr.onreadystatechange angular.js:8351

Upvotes: 2

Views: 8094

Answers (1)

javaCity
javaCity

Reputation: 4318

Check your json/and/or/pathtojson again your code works for me

http://plnkr.co/edit/AGdJ9SasmHn74RQe8xX2?p=preview

Upvotes: 3

Related Questions