学习JSON2
实验1-摄影作品目录
html部分的代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<script>
$.ajax({
type: "GET", //请求方式
url: "item.json", //url地址,就是json文件的路径
dataType: "json", //数据类型,可以是 text xml json script jsonp
success: function(result){ //result是响应信息返回的结果,此处包含了返回的json对象
addBox(result); //调用addBox函数,将result数据添加到box容器中
}
});
function addBox(result) {
//result是Json对象的集合,通过JQuery的each方法进行遍历,为每个对象生成div元素
$.each(result, function(index, obj) {
//添加一个class为product的容器,放置产品信息
$("#box").append("<div class='product'>" +
//获得图片地址
"<div><img class='img' src='" + obj['url'] + "'></div>" +
//获得名字
"<div class='p1 p'>" + obj['name'] + "</div>" +
//获得地址
"<div class='p2 p'>" + obj['address'] + "</div>" +
//获得作者
"<div class='p3 p'>" + obj['author'] + "</div>" +
"</div>");
});
}
</script>
<title>JSON示例</title>
<style type="text/css">
.p {
display: inline-block;
float: left;
width: 50%;
font-family: Microsoft YaHei;
}
.p1 {
font-size: 14px;
color: #000;
margin-top: 16px;
}
.p2 {
font-size: 12px;
color: #b0b0b0;
}
.p3 {
font-size: 14px;
color: #ff5f19;
}
.product {
width: 100%;
position: relative;
margin: 20px 0 5px 0;
height: 100px;
background: #fafafa;
}
.img {
width: 100px;
height: 100px;
float: left;
margin-right: 20px;
}
</style>
</head>
<body>
<!-- 构建装一个容器 -->
<div id="box">
</div>
</body>
</html>
json部分代码
[
{
"name":"桥",
"address":"地址1",
"author":"作者1",
"url":"./img/1.jpg"
},
{
"name":"大海星",
"address":"地址1",
"author":"作者2",
"url":"./img/2.png"
},
{
"name":"海滨日落",
"address":"地址1",
"author":"作者3",
"url":"./img/3.jpg"
},
{
"name":"日出",
"address":"地址1",
"author":"作者4",
"url":"./img/4.jpg"
},
{
"name":"桂林山水甲天下",
"address":"地址1",
"author":"作者5",
"url":"./img/5.jpg"
},
{
"name":"花田中的木屋",
"address":"地址1",
"author":"作者6",
"url":"./img/6.jpg"
}
]
实验2-虚假的天气预报
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div class="search">
<input type="text" name="city" id="city" placeholder="请输入查询天气的城市" />
</div>
<h2>北京天气预报</h2>
<p>最高气温:<span id="high1"></span></p>
<p>风力:<span id="fengli1"></span></p>
<p>最低气温:<span id="low1"></span></p>
<p>风向:<span id="fengxiang1"></span></p>
<p>天气:<span id="type1"></span></p>
<script>
var sites = [
{"date":"13日星期四","high":"高温 26℃","fengli":"<![CDATA[2级]]>","low":"低温 16℃","fengxiang":"东南风","type":"多云"},
{"date":"14日星期五","high":"高温 28℃","fengli":"<![CDATA[2级]]>","low":"低温 19℃","fengxiang":"东南风","type":"多云"},
{"date":"15日星期六","high":"高温 24℃","fengli":"<![CDATA[3级]]>","low":"低温 14℃","fengxiang":"东北风","type":"小雨"},
{"date":"16日星期天","high":"高温 25℃","fengli":"<![CDATA[2级]]>","low":"低温 15℃","fengxiang":"北风","type":"多云"},
{"date":"17日星期一","high":"高温 28℃","fengli":"<![CDATA[2级]]>","low":"低温 14℃","fengxiang":"北风","type":"晴"},
];
document.getElementById("high1").innerHTML=sites[0].high;
document.getElementById("fengli1").innerHTML=sites[0].fengli;
document.getElementById("low1").innerHTML=sites[0].low;
document.getElementById("fengxiang1").innerHTML=sites[0].fengxiang;
document.getElementById("type1").innerHTML=sites[0].type;
</script>
</body>
</html>