数据库的角度解析响应

时间:2016-09-22 17:31:43

标签: angularjs database response

我有来自数据库的回复:

{"status":"success","message":"Data selected from database","data":[{"id":1171,"sku":0,"word_one":"one word","description":"","word_two":"two word","mrp":0,"lang_one":"en","image":"","lang_two":"en","status":"Active","category":"[{\"text\":\"someone\"},{\"text\":\"sometwo\"}]","UserID":188},
...

{"id":1170,"sku":0,"word_one":"something","description":"","word_two":"some two","mrp":0,"lang_one":"en","image":"","lang_two":"en","status":"Active","category":"[{\"text\":\"ever\"},{\"text\":\"never\"}]","UserID":188}]}

在发帖之前:angular.toJson($scope.category);

我如何展示category这样的东西

{{category}} = someone, sometwo ?

因为实际上我有字符串:

[{"text":"someone"},{"text":"sometwo"}]
[{"text":"ever"},{"text":"never"}]

...

2 个答案:

答案 0 :(得分:0)

您可以使用以下解析来实现您想要的效果。假设json是从数据库接收的变量。

var json = {"status":"success","message":"Data selected from database","data":[{"id":1171,"sku":0,"word_one":"one word","description":"","word_two":"two word","mrp":0,"lang_one":"en","image":"","lang_two":"en","status":"Active","category":"[{\"text\":\"someone\"},{\"text\":\"sometwo\"}]","UserID":188}]};
var data = json.data[0].category;
var jsonArray = JSON.parse(data);
var category = "";
jsonArray.map(function(obj, i ) {
    if(i != 0) {
      category += ",";
    }
    category += obj.text; 
});
console.log(category)

最后,您将类别附加到$ scope.category。

答案 1 :(得分:0)

这项工作:

        var parsed = JSON.parse($scope.c.category);
        var arr = [];
        for(var x in parsed){
          arr.push(parsed[x]);
        }
        $scope.c.category = arr;