json与JavaScript对象互换

时间:2021-06-05 16:26:14

1,json字符串转化为JavaScript对象:

方法:JSON.parse(string)

eg:var account = '{"name":"jaytan","mebers":["cind","yobi"],\

"number":234,"location":"WUHAN"}';

var accountobj = JSON.parse(account);

console.log(account.name);

console.log(account.mebers);

输出结果:

json与JavaScript对象互换

2,JavaScript对象转化为JSON

方法:JSON.stringify(object)

eg:var accountObj = {

name:"jaytan",

number:3456,

members:["jim,bob"],

location:"wuhan"

};

var accountstr = JSON.stringify(accountobj);

console.log(accountstr);

输出结果:

json与JavaScript对象互换