In AngularJs , the following is working -
$scope.product= {items:[{itemname:'apple',itemdesc:'fruit'}]}
But I want to get $scope.product from json string as below -
var strJson = "{items:[{itemname:'apple',itemdesc:'fruit'}]}";
$scope.product=strJson;
is not working. All I have is a json string and I need to assign to $scope.product. I have used JSON.parse() and JSON.toJson(), but they are not working.
I don't know what I'm doing wrong
Below is my exact code :
$http.get('/getItems').success(function(data) {
var jsondata="{\items\:";
jsondata+=JSON.stringify(data);
jsondata+="}";
jsondata=jsondata.replace(/"itemname"/g, 'itemname');
jsondata=jsondata.replace(/"itemdesc"/g, 'itemdesc');
// WORKING CODE
$scope.product = {items:[{itemname:"apple",itemdesc:"fruit"}]};
// NOT WORKING CODE
var jsonObj = jsondata;
$scope.product = jsonObj;
});