如何使用节点删除响应中的额外斜杠?

时间:2017-11-20 05:16:07

标签: node.js

如何删除回复中的额外斜杠?

我已尝试JSON.parseJSON.stringify,但他们的代码无效。 JSON.parse会引发错误,例如json at position 10

我在一个数组中推送了响应对象。然后我在响应中显示了对象数组(带有数组)。

{
"status": true,
"message": "Data Found",
"data": [
"{\"errors\":[],\"detail\":[{\"repositories\":[],\"_instance\":{\"applicationLinkId\":\"4b0d5edc-c683-3502-aed7-5f6e152b877d\",\"singleInstance\":false,\"primary\":true,\"baseUrl\":\"http://stash.computenext.com\",\"name\":\"Stash\",\"typeName\":\"Bitbucket Server\",\"id\":\"4b0d5edc-c683-3502-aed7-5f6e152b877d\",\"type\":\"stash\"}}]}" 
] 
}

我的代码:

exports.getCommits = function (req, res) {
  console.log(filename + '>>get commits>>');
  var response = {
    status: Boolean,
    message: String,
    data: String
  };
  var request = require('request');
  var username = username;
  var password = password;
  var options = {
    url: 'https://computenext.atlassian.net/rest/api/2/search?jql=status+%3D+Resolved+ORDER+BY+updated',
    auth: {
      username: username,
      password: password
    }
  };

  request(options, function (error, obj) {
    if (error) {
      response.message = appmsg.DATA_NT_FOUND;
      response.status = false;
      response.data = obj;
      res.send(response);
    } else {
      response.message = appmsg.DATA_FOUND;
      response.status = true;
      response.data = JSON.parse(obj.body);
      //res.send(response);
      var respon = {
        status: Boolean,
        message: String,
        data: String
      };

      var issueKey = response.data.issues;
      var id = issueKey[0].id;
      console.log(id);

      var commitout = [];
      for (var i = 0; i < issueKey.length; i++) {
        var commits = issueKey[i].id;
        console.log(commits);

        var request = require('request'),
          username = username,
          password = password,
          url =
          "https://computenext.atlassian.net/rest/dev-status/1.0/issue/detail?issueId=" +
          commits + "&applicationType=stash&dataType=repository",
          auth = "Basic " + new Buffer(username + ":" + password).toString(
            "base64");

        //console.log(url);
        var test = [];
        request({
          url: url,
          headers: {
            "Authorization": auth
          }
        }, function (err, obj1) {
          if (obj1) {

            var info1 = obj1.body;
            commitout.push(info1);

            if (issueKey.length === commitout.length) {
              respon.message = appmsg.DATA_FOUND;
              respon.status = true;
              respon.data = commitout;
              res.send(respon);
            }
          }
        });
      }
    }
  });
};

1 个答案:

答案 0 :(得分:0)

尝试解析以下元素

commitout.push(info1);

更改为,

commitout.push(JSON.parse(info1));

<强>更新

尝试以下正则表达式

info1 = info1.replace(/\\/g, "");
commitout.push(info1);