Join the Stack Overflow Community
Stack Overflow is a community of 6.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

i'm newbie to Angular. I'm trying to get Json data which written like that :

id:1,
name: "Friends",
type: "Group",

Now, i know i can't use this kind of data unless i add double quotes - like that (it's the only way it is working):

"id":1,
"name": "Friends",
"type": "Group",

What is the best way to parse JSON in Angular?

I've tried to combine JS , but it didn't work :

app.controller('contacts', function ($scope,$http,$log) {

    $http.get('http://localhost/Angular-Http-exercise/db.json')
        .then(function(response) {
            $scope.myData = function() {

            return JSON.stringify(response.data)
        };

        $log.info(response.data.name);
    });
});
share|improve this question
    
make sure the source is valid json in the first place. If the contents of that file don't validate on jsonlint.com...fix it. Beyond that this question doesn't make much sense – charlietfl 52 mins ago
    
charlietfl -thanks – RoyBar 34 mins ago
up vote 1 down vote accepted

You can use angular.fromJson and angular.toJson for that purpose:

scope.myData = function(){
       return angular.fromJson(response.data);   
}

However JSON is a global variable and you should have access to it, since angular uses the same.

share|improve this answer

Roy, your question is What is the best way to parse JSON in Angular? JSON will not differ from one framework/language to another and there's no best way to write JSON.

If you want to see if your JSON is formatted correctly check out tools like: https://jsonformatter.curiousconcept.com/

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.