日期:2014-05-16 浏览次数:20345 次
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"/> <script type="application/x-javascript"> function print(str){ document.write(str+"<br/>"); } //下面是原生javascript对象 var book = { name:"felayman", age:22, scholl:"天津大学", hobby:"篮球" }; print(book);//输出为object //将javascript对象转换成JSON字符串 var jsonTeXt = JSON.stringify(book); print(jsonTeXt);//输出为{"name":"felayman","age":22,"scholl":"天津大学","hobby":"篮球"} print(jsonTeXt.name+jsonTeXt.age);//这样得不到值 //将json字符串解析为一个原生javascript对象 var person = JSON.parse(jsonTeXt); print(person.name+person.age+person.scholl+person.hobby); //JSON.stringify()的两个参数 var per1 = JSON.stringify(book,["name","age"]); print(per1); //自定义toJSON()方法 var Person = { "name":"felayman", "age":22, "hobby":"篮球", toJSON : function(){ return this.age; } }; print(Person.toJSON()); </script> </head> <body> </body> </html>