我怎样才能在Angular中使用JSON stringify?

时间:2017-02-09 23:15:18

标签: angularjs json

我是Angular的新手。我试图获得那样写的Json数据:

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

现在,我知道我不能使用这种数据,除非我添加双引号 - 就像那样(它是唯一的工作方式):

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

在Angular中解析JSON的最佳方法是什么?

我试图结合JS,但它没有工作:

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

2 个答案:

答案 0 :(得分:2)

您可以将angular.fromJsonangular.toJson用于此目的:

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

然而,JSON是一个全局变量,您应该可以访问它,因为angular使用相同的。

答案 1 :(得分:0)

Roy,你的问题是What is the best way to parse JSON in Angular? JSON在一个框架/语言与另一个框架/语言之间没有区别,并且没有最好的方法来编写JSON。

如果您想查看您的JSON格式是否正确,请查看以下工具:https://jsonformatter.curiousconcept.com/

相关问题