2

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;
});

6 Answers 6

4

JSON standard expects values to have double quotes around it. So your JSON string isn't parsable JSON.

var mystr = '{"items": [{"itemname": "apple","itemdesc": "fruit"}]}';
console.log(JSON.parse(mystr))
console.log(typeof JSON.parse(mystr))

Sign up to request clarification or add additional context in comments.

1 Comment

$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; });
1

I will suggest to use angular way to conver json to object:

$scope.product= "{items:[{itemname:'apple',itemdesc:'fruit'}]}";
$scope.product = angular.fromJson($scope.product);

And object to json as follow:

var strJson = angular.toJson($scope.product);

Comments

1

Here is the problem. apparently, if you are using JSON.parse to convert a JSON string to JSON then your property names should be wrapped with double quotes instead of single quotes.

angular.module("app",[])
.controller("ctrl",function($scope){

$scope.product= '{"items":[{"itemname":"apple","itemdesc":"fruit"}]}'
    $scope.product = JSON.parse($scope.product);
    
console.log($scope.product)
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
 
</div>

2 Comments

I just edited the answer. hope the downvoter will review this
Myexact problem is this : $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; });
0

You can use JSON.parse, find the answer below

var strJson = JSON.parse("{items:[{itemname:'apple',itemdesc:'fruit'}]}");

Comments

0

I think your string format is not correct, it should be like this.

var str = '{"items":[{"itemname":"apple","itemdesc":"fruit"}]}';
$scope.product = JSON.parse(str)

Comments

0

please use double quotes in the JSON (by escaping them). Then JSON.parse() will accept the input string appropriately and parse correctly. Also remove square brackets.

$scope.jsonString = "{\"items\":{\"itemname\":\"apple\",\"itemdesc\":\"fruit\"}}";

var test = JSON.parse($scope.jsonString);

on a side note you can also use JSON Lint to check if your JSON is valid.

Let me know if this helps.

3 Comments

your parent node 'items' in the line - var jsondata="{\items\:"; does not contain double quotes. please try var jsondata="{\"items\":";. Also please check subsequent values to place double quotes. There is the escaping '\' character but not the character you will be escaping.
Please read the question once more. $scope.product = {items:[{itemname:"apple",itemdesc:"fruit"}]}; is working code
Your object works fine I get it. I was talking about the json 'STRING' which is giving you parsing errors - and which is your requirement am i correct ? The double quotes at the right places should solve the issue of parsing it. I tried it using JSON.parse() and it seems to be working fine. Let me know if I am missing something.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.