Buffer对象与JSON对象相互转换


> buffer=new Buffer('换汤不换药');
<Buffer e6 88 91 e7 88 b1 e4 bd a0 ef bc 8c e7 89 a9 e7 90 86>
> json=JSON.stringify(buffer);
'{"type":"Buffer","data":[230,136,145,231,136,177,228,189,160,239,188,140,231,13
7,169,231,144,134]}'
> JSON.parse(json);
{ type: 'Buffer',
data:
[ 230,
136,
145,
231,
136,
177,
228,
189,
160,
239,
188,
140,
231,
137,
169,
231,
144,
134 ] }
> copy=new Buffer(JSON.parse(json));
<Buffer e6 88 91 e7 88 b1 e4 bd a0 ef bc 8c e7 89 a9 e7 90 86>
> copy.toString();
'换汤不换药'
>

 

说明:

(1)JSON.stringify方法将数据对象转换成一个字符串

(2)JSON.parse方法将字符串转换成对象

posted @ 2019-08-15 11:03  蔚京霖  阅读(4437)  评论(0编辑  收藏  举报