I'm trying to replace the angularjs POST query with standalone JSON response string.
When angular GET / POST queries returns a response automatically converted to JSON and the code was working like charm.
Now, I'm trying to have the json response stored as a javascript string variable in the controller and then trying to parse it using JSON.stringify()
and subsequently using JSON.parse()
.
There is no error but the resulting json object's member variables can't be accessed using the .
operator
var staticData = '{"someKey":"someValue", "masterJobs":[]}'; //very large json string.
var resultString = JSON.stringify(staticData);
$scope.staticTestData = JSON.parse(resultString);
console.log($scope.staticTestData.masterJobs); // this displays 'undefined'
Controller function with the large JSON is available here.
SyntaxError: Unexpected token in JSON at position 1323 at JSON.parse (
DISPLAYED
\n
\nPassed"`. It contains new lines `\n` which will cause your string to actually have new lines in it (and not inside the value). You need to escape them, see https://stackoverflow.com/questions/11591784/parsing-json-containing-new-line-characters for details. – str Aug 01 '17 at 08:27