上代码
//创建缓冲区
let bufferOne = Buffer.from('Hello World');
console.log(JSON.stringify(bufferOne));
//输出 {"type":"Buffer","data":[72,101,108,108,111,32,87,111,114,108,100]}
//将 Buffer 转换为 UTF-8 字符串
console.log(bufferOne.toString('utf8'))
//输出 Hello World
//缓冲区可以转换为 JSON。
let json = JSON.stringify(bufferOne, null, 2)
console.log(JSON.stringify(json))
//输出 "{\n \"type\": \"Buffer\",\n \"data\": [\n 72,\n 101,\n 108,\n 108,\n 111,\n 32,\n 87,\n 111,\n 114,\n 108,\n 100\n ]\n}"
//将 JSON 转换为 Buffer
let bufferOriginal = Buffer.from(JSON.parse(json).data)
console.log(JSON.stringify(bufferOriginal))
//输出 {"type":"Buffer","data":[72,101,108,108,111,32,87,111,114,108,100]}