六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 34|回复: 0

Prototype支持JSON

[复制链接]

升级  30.33%

87

主题

87

主题

87

主题

举人

Rank: 3Rank: 3

积分
291
 楼主| 发表于 2013-1-29 11:39:28 | 显示全部楼层 |阅读模式
JSON格式的数据具有轻量级,易懂,跨语言的优势,Prototype库的JSON支持Date,Object,Array,Hash,Number类增加里toJSON()方法。<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>JSON支持</title><meta name="author" content="Yeeku.H.Lee" /><meta name="website" content="http://www.crazyit.org" /><meta http-equiv="Content-Type" content="text/html; charset=GBK" /></head><body><script src="js/prototype-1.6.0.3.js" type="text/javascript"></script><script type="text/javascript">var date = new Date();//将Date对象转化成JSON字符串document.write("日期对应的JSON字符串为:" + date.toJSON() + "<br>");var p = {name : "lbx",age : 30};//将对象转换成JSON字符串document.write("普通对象的JSON字符串为:" + Object.toJSON(p) + "<br>");var books = [ "Java", "Ajax" ];//将数组转换成JSON字符串document.write("数组的JSON字符串为:" + books.toJSON() + "<br>");var hash = $H( {name : 'lbx',age : 30});//将Hash对象转换成JSON字符串document.write("Hash对象的JSON字符串为:" + hash.toJSON() + "<br>");//将数值转换成JSON字符串document.write("数值的JSON字符串为:" + (45).toJSON() + "<br>");//下面四行代码测试怎样才算合法的字符串document.write('"lbx".isJSON()的结果为:' + "lbx".isJSON() + "<br>");document.write('"\"lbx\"".isJSON()的结果为:' + "\"lbx\"".isJSON()+ "<br>");document.write('"{age: 30}.isJSON()的结果为:' + "{age: 30}".isJSON()+ "<br>");document.writeln('"{\"age\": 30}".isJSON()的结果为:' + "{\"age\": 30}".isJSON()+ "<br />");//定义一个JSON格式的字符串var str = '{"name": "lbx", "age": 30}';//将JSON格式的字符串转换成JSON对象var author = str.evalJSON();document.writeln('author对象的name值为:' + author.name + "<br />");document.writeln('author对象的age值为:' + author.age + "<br />");</script></body></html> 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表