浅谈JSON.parse()、JSON.stringify()和eval()的作用

(1)JSON.parse 函数

 

var json = '{"name":"GDT","age":23,"University":"GDUT"}';
var info = JSON.parse(json);  //解析为JSON对象
document.write(info.name + ' is a student of ' + info.University + ' and he is ' + info.age + " years old."); /info为Object对象

 

(2)JSON.stringify()函数

 

var info = {name:"GDT",age:23,University:"GDUT"};
var json = JSON.stringify(info); //转换为JSON字符串
document.write(json); //output为{"name":"GDT","age":23,"University":"GDUT"}

 

(3)eval()函数

 

var json = '{"name":"GDT","age":23,"University":"GDUT"}';
var info = eval('(' + json + ')');  //解析为JSON对象
document.write(info);            //output为[object Object]

 

posted @ 2016-04-12 16:00  归尘2016  阅读(482)  评论(1编辑  收藏  举报