根据json ,读取json中的people列表的第二个,根据相应的key,把对应的value显示在div中,用jquery实现
有如下jsonvarp='{"people":[{"firstName":"Brett","lastName":"Mclaughlin","email":"brett@n...
有如下 json
var p='{"people":[
{ "firstName":"Brett",
"lastName":"Mclaughlin",
"email":"brett@newInstance.com",
}
{ "firstName":"jsone",
"lastName":"Hunter",
"email":"jsone@servlets.com",
}
{ "firstName":"Elliotter",
"lastName":"Harold",
"email":"brett@newInstance.com",
}
]}'
<html>代码结构如下:
<div class='firstName'></div>
<div class='secondname'></div>
<div class='email'></div> 展开
var p='{"people":[
{ "firstName":"Brett",
"lastName":"Mclaughlin",
"email":"brett@newInstance.com",
}
{ "firstName":"jsone",
"lastName":"Hunter",
"email":"jsone@servlets.com",
}
{ "firstName":"Elliotter",
"lastName":"Harold",
"email":"brett@newInstance.com",
}
]}'
<html>代码结构如下:
<div class='firstName'></div>
<div class='secondname'></div>
<div class='email'></div> 展开
3个回答
展开全部
var p='{"people":[
{ "firstName":"Brett",
"lastName":"Mclaughlin",
"email":"brett@newInstance.com",
}
{ "firstName":"jsone",
"lastName":"Hunter",
"email":"jsone@servlets.com",
}
{ "firstName":"Elliotter",
"lastName":"Harold",
"email":"brett@newInstance.com",
}
]}';
//原生
function buildViewer( ){
var formatP = eval( '(' + p + ')' );//字符串->对象
var people = formatP.people;
var html = [];
for( var i = 0 , L = people.length; i < L ; i++ ){
var c = people[i];
var firstName = c.firstName,
lastName = c.lastName,
email = c.email;
html.push( "<div class='firstName'>" , firstName , "</div>" );
html.push( "<div class='secondname'>", lastName ,"</div>" );
html.push( "<div class='email'>", email ,"</div>" );
}
return html.join('');
}
//使用jQuery
function builViewer(){
var formatP = eval( '(' + p + ')' );//字符串->对象
var people = formatP.people;
var html = [];
$.each( people , function( i , item ){
var firstName = item.firstName,
lastName = item.lastName,
email = item.email;
html.push( "<div class='firstName'>" , firstName , "</div>" );
html.push( "<div class='secondname'>", lastName ,"</div>" );
html.push( "<div class='email'>", email ,"</div>" );
});
return html.join('');
}
展开全部
你这个淘气的孩子,多半人会把时间花在帮你调整json数据上。
<script type="text/javascript">
$(function(){
var p={"people":[
{ "firstName":"Brett",
"lastName":"Mclaughlin",
"email":"brett@newInstance.com"
},
{ "firstName":"jsone",
"lastName":"Hunter",
"email":"jsone@servlets.com"
},
{ "firstName":"Elliotter",
"lastName":"Harold",
"email":"brett@newInstance.com"
}
]};
$.each(p.people,function(key,value){
alert(value.lastName);
});
});
</script>
<script type="text/javascript">
$(function(){
var p={"people":[
{ "firstName":"Brett",
"lastName":"Mclaughlin",
"email":"brett@newInstance.com"
},
{ "firstName":"jsone",
"lastName":"Hunter",
"email":"jsone@servlets.com"
},
{ "firstName":"Elliotter",
"lastName":"Harold",
"email":"brett@newInstance.com"
}
]};
$.each(p.people,function(key,value){
alert(value.lastName);
});
});
</script>
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
var obj = p.people[1];
$("div.firstName").text(obj.firstName);
$("div.secondname").text(obj.lastName);
$("div.email").text(obj.email);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询