将javascript对象转换为json对象

时间:2014-11-05 21:55:38

标签: javascript json loggly

我正在创建一个logMessage对象。

logMessage = function (msg, severity, vendorId, userName, actionPerformed, priority, traceId, dataSent) {
    this.message = msg;
    this.severity = severity;
    this.vendorId = vendorId;
    this.userName = userName;
    this.actionPerformed = actionPerformed;
    this.priority = priority;
    this.traceId = traceId;
    this.dataSent = dataSent;
};

var msg = new logMessage(err, "High", "none", qry.username, "Error on login call: /req/login", "high", "", qry);
Utility.writeToLoggly(msg);

err ,qry是json对象;如何将msg对象转换为json对象? 我正在将msg对象发送到loggly以进行日志管理。如果我能将正确格式化的json对象发送到loggly,那就太棒了。

2 个答案:

答案 0 :(得分:3)

logMessage = function (msg, severity, vendorId, userName, actionPerformed, priority, traceId, dataSent) {
    this.message = msg;
    this.severity = severity;
    this.vendorId = vendorId;
    this.userName = userName;
    this.actionPerformed = actionPerformed;
    this.priority = priority;
    this.traceId = traceId;
    this.dataSent = dataSent;
};

var msg = new logMessage(err, "High", "none", qry.username, "Error on login call: /req/login", "high", "", qry);
Utility.writeToLoggly(JSON.stringify(msg));

答案 1 :(得分:1)

你可以使用JSON.stringify()函数,所以只需添加到你的代码中:

Utility.writeToLoggly(JSON.stringify(msg));