从angularjs中删除JSON中的Slash

时间:2015-09-10 05:49:30

标签: json angularjs

我从JSON.stringify()之后的web服务中获取一个JSON对象。我得到了这个输出 - “

[
    {
        "user": "A220",
        "shorttext": "shanghai",
        "reportedBy": "S,A",
        "questions": "[{\"question\":\"Q1\",\"is_mand\":\"0\",\"type\":\"text\",\"answer\":\"w\",\"ansYesOrNo\":false,\"ansDetails\":\"\"},{\"question\":\"Q2\",\"is_mand\":\"0\",\"type\":\"text\",\"answer\":\"ed\",\"ansYesOrNo\":false,\"ansDetails\":\"\"}]",
        "notifno": "20143995",
        "error": "",
        "createdOn": "2015-09-09 13:08:36",
        "Id": 0,
        "$$hashKey": "object:89"
    }
]

`

我想删除所有这些反斜杠。 我尝试使用

var a=JSON.stringify(<that object>).replace(/\\/g, "");

但是它给出的结果是一个像这样的INVALID的表单字符串 -

[
{
    "user": "A220",
    "shorttext": "shanghai",
    "reportedBy": "S,A",
    "questions": "[{"question":"Q1","is_mand":"0","type":"text","answer":"w","ansYesOrNo":false,"ansDetails":""},{"question":"Q2","is_mand":"0","type":"text","answer":"ed","ansYesOrNo":false,"ansDetails":""}]",
    "notifno": "20143995",
    "error": "",
    "createdOn": "2015-09-09 13:08:36",
    "Id": 0,
    "$$hashKey": "object:89"
}

我唯一的目标是在不更改数据类型的情况下删除反斜杠。 我想访问第一个问题。 它不起作用...... JsFiddle链接 - LINK

1 个答案:

答案 0 :(得分:0)

您不需要在Web服务响应中执行JSON.stringify()。你可以像

一样直接工作
$http.get('/someUrl').
then(function(response) 
{
   $scope.data = response;
}, 
function(response) 
{
    // called asynchronously if an error occurs
    // or server returns response with an error status.
});

现在你打印了像{{data}}

这样的html