How to convert the buffer inNodejsto JSON format and UTF-8 string? The following article will introduce to you the method of converting buffer and JSON formats in Nodejs, as well as the method of converting Buffer to UTF-8 string.

Node.js is different from browser-based JavaScript because Node had a way to handle binary data even before the ES6 draft proposedArrayBuffer. In Node, theBuffer(buffer)class is the primary data structure used by most I/O operations. It is raw binary data allocated outside the V8 heap and cannot be resized once allocated. [Recommended learning: "nodejs Tutorial"]
Before Node v6.0, to create a new buffer, we only need to use thenewkeyword to call the constructor Function:
let newBuff = new Buffer('I Love You')
After v6.0, to create a new buffer instance:
let newBuff = Buffer.from('I Love You')
new Buffer()The constructor has been deprecated and replaced by a separateBuffer.from(),Buffer.alloc()andBuffer.allocUnsafe()method replacement.
For more information, you can readOfficial Document.
Buffers can be converted to JSON.
let bufferOne = Buffer.from('All work and no play makes Jack a dull boy') console.log(bufferOne) // let json = JSON.stringify(bufferOne, null, 2) console.log(json) /* { "type": "Buffer", "data": [ 65, 108, 108, 32, 119, 111, 114, 107, 32, 97, 110, 100, 32, 110, 111, 32, 112, 108, 97, 121, 32, 109, 97, 107, 101, 115, 32, 74, 97, 99, 107, 32, 97, 32, 100, 117, 108, 108, 32, 98, 111, 121 ] } */
JSON Specifies that the type of object to be converted isBufferand its data.
let bufferOriginal = Buffer.from(JSON.parse(json).data) console.log(bufferOriginal) //
console.log(bufferOriginal.toString('utf8')) // All work and no play makes Jack a dull boy
. toString()is not the only way to convert a buffer to a string. Additionally, by default, it is converted to a utf-8 format string.
Another way to convert a buffer to a string is to use theStringDecodercore module in the Node.js API.
string_decoderThe module provides a way to decode aBufferobject into a string (to preserve encoded multibyte UTF-8 and UTF -16 character mode) API. An alternative to the above example is as follows:
const { StringDecoder } = require('string_decoder') const decoder = new StringDecoder('utf8') let bufferOriginal = Buffer.from(JSON.parse(json).data) console.log(decoder.write(bufferOriginal)) // All work and no play makes Jack a dull boy console.log(decoder.end(bufferOriginal)) // All work and no play makes Jack a dull boy
When theBufferinstance is written to theStringDecoderinstance, the internal buffer is used to ensure that the decoded string is not Contains any incomplete multibyte characters. These are kept in the buffer until the next call tostringDecoder.write()or the next call tostringDecoder.end().
For more programming related knowledge, please visit:Programming Video! !
The above is the detailed content of How to convert buffer in Nodejs to JSON format and UTF-8 string?. For more information, please follow other related articles on the PHP Chinese website!
node.js debugging
How to open json format
js string to array
WiFi shows no access to the Internet
How to connect asp to access database
How to retain the number of decimal places in C++
What is the reason why the computer screen is black but the computer is on?
What to do if your IP address is attacked