我正在使用接受NDJSON的简单TCP服务器(本质上由'\ r \ n'分隔)的应用程序的node.js API。无论如何,我遇到了JSON stringify的问题。我用变量创建一个对象(见下文),然后将其转换为stringify。当我得到结果时,它扩展了除“requestParamName”之外的所有变量。这是唯一的关键,而不是作为一个价值,这是结果:Node.js JSON.stringify变量不扩展
{"jsonrpc":"2.0","method":"Client.SetVolume","id":0,"params":{"client":"00:00:00:00:00:00","requestParamName":10}}
它应该是这样的:
{"jsonrpc":"2.0","method":"Client.SetVolume","id":0,"params":{"client":"00:00:00:00:00:00","volume":10}}
我已经试过几件事情,但我不知道是什么导致它不能扩展这个特定的变量。如果有人有任何建议,我会很感激。
功能:
function ClientConnect(requestMethod, requestMacAddress, requestParamName, requestParamKey) {
var objectRequest = {
"jsonrpc": "2.0",
"method": requestMethod,
"id": 0,
"params": {
"client": requestMacAddress,
requestParamName: requestParamKey
}};
formattedJson = (JSON.stringify(objectRequest) + '\r\n');
console.log(formattedJson);
}
附:我是新来的,所以如果我搞砸了格式化,我道歉;)
以上哇,我不知道有这个东西括号没有。它非常完美!万分感谢! –