如何在不获取额外反斜杠字符的情况下将JSON打印为字符串?

时间:2019-02-13 12:23:27

标签: javascript json

此代码:

req.reject({
    name: 'Server returned error',
    message: (`${message.errorcode}${message.data ? `, data: ${JSON.stringify(message.data)}` : ''}`)
}); 

产生带有不需要的\字符的输出:

{ 
    name: 'Server returned error',
    message: 'Command failure, data: {"message":"No device with name \'aAASDASDA\' connected."}' 
}

此代码:

req.reject({
    name: 'Server returned error',
    message: (`${message.errorcode}${message.data ? `, data: ${message.data}` : ''}`)
}); 

产生隐藏键和值的输出

{ 
    name: 'Server returned error',
    message: 'Command failure, data: [object Object]' 
}

如何生成带有键和值但没有多余\字符的输出

{ 
    name: 'Server returned error',
    message: 'Command failure, data: {"message":"No device with name 'aAASDASDA' connected."}' 
}

0 个答案:

没有答案