Adi Das
Adi Das

Reputation: 21

Large JSON String not parsing using JSON.parse or angular.fromJson

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.

Upvotes: 1

Views: 891

Answers (1)

str
str

Reputation: 44979

You already have a string, so there is no need to use JSON.stringify.

Just use the following code:

var staticData = '{"someKey":"someValue", "masterJobs":[]}'; //very large json string.
$scope.staticTestData = JSON.parse(staticData);
console.log($scope.staticTestData.masterJobs);

Upvotes: 3

Related Questions