{
"data": {
"info": [
{
"username": "john",
"uid": "893y4t89hg98y7894th",
"avatar": null,
"level": null
},
{
"username": "joel",
"uid": "ui9otjry4t89hg994t",
"avatar": null,
"level": null
}
]
}
}
这是json数据。这是jsfiddle。
JS:
$(function(){
var str = '{"data":{"info":[{"username":"john","uid":"893y4t89hg98y7894th","avatar":null,"level":null},{"username":"joel","uid":"ui9otjry4t89hg994t","avatar":null,"level":null}]}}';
str = JSON.parse(str.data.info);
$.each(str, function(i, item){
$(".result1").append(item.username);
$(".result2").append(item.uid);
});
});
有人可以告诉我我做错了什么吗?
有点不对劲。您不能将字符串作为对象访问。
str = JSON.parse(str.data.info); //str is string, no object
$.each(str, function(i, item){
//... it wrong
str = JSON.parse(str);//parse string, get object
$.each(str.data.info, function(i, item){
//... it right
见演示
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句