node.js JS对象和JSON字符串之间的转换

时间:2023-12-09 22:01:31

JSON.stringify(obj)将JS对象转为字符串

var json = { aa: ['sdddssd'],
  bb: [ '892394829342394792399', '23894723984729374932874' ],
  cc: ['11111111111111'] }

=> var string = JSON.stringify(json)

string:

{"aa":["sdddssd"],"bb":["892394829342394792399","23894723984729374932874"],"cc":["11111111111111"]}

JSON.parse(string)将字符串转为JS对象

JSON.parse(string):

=>

{ aa: ['sdddssd'],
  bb: [ '892394829342394792399', '23894723984729374932874' ],
  cc: ['11111111111111'] }

You can simply use JSON.parse.

node.js is built on V8, which provides the global object JSON[docs]. The definition of the JSON object is part of the ECMAScript 5 specification.