HTML5已原生支持json的解析,window.JSON.parse()将json格式字符串转换为json对象,window.JSON.stringify()将json对象转换为json格式字符串。
示例:Html代码
<!DOCTYPE HTML>
<html>
<head>
<title>Window.JSON</title>
<meta charset="gb18030">
</head>
<body>
<button type="button" id="btn1">解析json字符串</button>
<button type="button" id="btn2">json对象转换为json字符串</button>
<div id="res">
</div>
<script language="JavaScript">
<!--
var jsonStr = "{\"total\":100,\"data\":[{\"id\":10001,\"name\":\"scott\"},{\"id\":10002,\"name\":\"tiger\"}]}"
var jsonObj = window.JSON.parse(jsonStr)
document.getElementById("btn1").onclick = function() {
var str = "json字符串解析为json对象<br>"
str += "<span>Total:"+jsonObj.total+"</span><br><span>Data:"
for (var i=0i<jsonObj.data.length i++)
{
str += "id:" + jsonObj.data[i].id + ",name:" + jsonObj.data[i].name+"<br>"
}
str += "</span><br>"
document.querySelector("#res").innerHTML = str
}
document.getElementById("btn2").onclick = function() {
var jsonObj = {total:100,data:[{id:10001,name:"scott"},{id:10002,name:"tiger"}]}
var jsonStr = window.JSON.stringify(jsonObj)
var str = "转为json字符串:<br>" + jsonStr
document.querySelector("#res").innerHTML = str
}
//-->
</script>
</body>
</html>
单击“解析json字符串”按钮,结果:
json字符串解析为json对象
Total:100
Data:id:10001,name:scott
id:10002,name:tiger
单击“json对象转换为json字符串”按钮,结果:
转为json字符串:
{"total":100,"data":[{"id":10001,"name":"scott"},{"id":10002,"name":"tiger"}]}
json文件是一种轻量级的数据交互格式。一般在jquery中使用getJSON()方法读取。$.getJSON(url,[data],[callback])
url:加载的页面地址
data: 可选项,发送到服务器的数据,格式是key/value
callback:可选项,加载成功后执行的回调函数
1.首先建一个JSON格式的文件userinfo.json 保存用户信息。如下:
?
1234567891011121314151617
[{"name":"张国立","sex":"男","email":"zhangguoli@123.com"},{"name":"张铁林","sex":"男","email":"zhangtieli@123.com"},{"name":"邓婕","sex":"女","email":"zhenjie@123.com"}]
2.其次建一个页面用于获取JSON文件里的用户信息数据,并显示
?
123456789101112131415161718192021222324252627282930313233343536373839404142
<!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><meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" /><title>getJSON获取数据</title><script type="text/javascript" src="js/jquery-1.8.2.min.js"></script><style type="text/css">#divframe{ border:1px solid #999width:500pxmargin:0 auto}.loadTitle{ background:#CCCheight:30px}</style><script type = "text/javascript" >$(function (){ $("#btn").click(function () {$.getJSON("js/userinfo.json", function (data){ var $jsontip = $("#jsonTip") var strHtml = "123" //存储数据的变量 $jsontip.empty() //清空内容 $.each(data, function (infoIndex, info){strHtml += "姓名:" + info["name"] + "<br>" strHtml += "性别:" + info["sex"] + "<br>" strHtml += "邮箱:" + info["email"] + "<br>" strHtml += "<hr>" }) $jsontip.html(strHtml) //显示处理后的数据}) })})</script></head><body><div id="divframe"><div class="loadTitle"><input type="button" value="获取数据" id="btn"/></div><div id="jsonTip"></div></div></body></html>
<!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>
<meta http-equiv="Content-Type" content="text/html charset=utf-8" />
<title>获取JSON对象的属性值</title>
<script type="text/javascript">
function strToObject()
{
var object = {"name":"zhangsan","sex":"男","age":"29"}
for(var obj in object)
{
alert(object[obj])
}
}
</script>
</head>
<body>
<div id="body_div" style="vertical-align:middle text-align:center">
<input type="button" id="btn" value="转换" onclick="strToObject()"/>
</div>
</body>
</html>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)