javascript 如何在节点js中将对象转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31603232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 14:02:31  来源:igfitidea点击:

How do I convert object to string in node js

javascriptnode.jsclient-server

提问by Angela

I am very new to node js. I am trying to create a simple coap client and server using node js. I could create a server on which a text file resides. I want to access that from the client.

我对 node js 很陌生。我正在尝试使用 node js 创建一个简单的 coap 客户端和服务器。我可以创建一个文本文件所在的服务器。我想从客户端访问它。

var coap = require('coap');
var str = "";
var req = coap.request("coap://localhost:5683");
req.on("response", function(chunk){
    str +=chunk;    
    console.log(str);   
    chunk.pipe(process.stdout);
});

This code is giving me the output as [object Object]. How do I get the string form of this.

这段代码给我的输出是 [object Object]。我如何获得这个的字符串形式。

回答by skypHyman

You can use JSON.stringifyand JSON.parseto do that. Herethe documentation.

您可以使用JSON.stringifyJSON.parse来做到这一点。这里的文档。