A. JQuery遍历jsON看看下面这个怎么取值,取name,title,content如果这样的字符串不行的话,看看怎么改
你从后台组合字符串时最好改成这种格式:(属性也最好用引号引起来)
[{‘回name’:'name1','title':'title1','content':'content1'},{’name‘:'name2','title':'title2','content':'content2'}]
然后在页答面用一个变量接收:
var articles = 获取的后台传来的值
遍历时操作最好先将其转换成json格式,因为从后台获取的是一个字符串:
articles = eval('('+ articles +')');
遍历:
$(articles).each(function(){
alert(this.name);
alert(this.title);
alert(this.content);
});
B. jquery 如何遍历这样的json
方法挺多的把,可以用for循环,也可以用each方法。
先献上ajax请求:
$.ajax({
url:'/path/to/file',
type:'GET',
dataType:'json',
data:{param1:'value1'},
success:function(obj){
//遍历obj
}
})
返回的内容在success的函数里面,所有的遍历操作都是在这里面操作的:
for循环:
varobj={
"status":1,
"bkmsg":"u6210u529f",
"bkdata":["u5415u5c1au5fd7","1387580400","u6dfbu52a0u8bb0u5f55"]
}
//console.log(obj.length);
if(obj.status==1){
for(vari=0;i<obj.bkdata.length;i++){
console.log(obj.bkdata[i]);
};
}else{
alert("数据有误~");
};
for in 循环:
//forin循环
for(xinobj.bkdata){
//x表示是下标,来指定变量,指定的变量可以是数组元素,也可以是对象的属性。
console.log(obj.bkdata[x]);
}
//元素 each方法
if(obj.status==1){
$(obj.bkdata).each(function(index,item){
//index指下标
//item指代对应元素内容
//this指代每一个元素对象
//console.log(obj.bkdata[index]);
console.log(item);
//console.log($(this));
});
}else{
alert("数据有误~");
};
//jquery each方法
$.each(obj.bkdata,function(index,item){
console.log(item);
});
C. jquery遍历民族json数据
/*
*我用是jquery,javascript原生版,看注释掉的代码
*/
//varjsNationNameContent=document.getElementById('nationNameContent');
varnationNameContent=$('#nationNameContent'),
html=[];
for(vari=0,len=nationItems.length;i<len;i++){
varnation=nationItems[i];
varid=nation.id;
varname=nation.name;
//你要生成什么样的格式?我这里就搞个简单的span标签
//拼装html
html.push('<span>民族编号:'+id+'</span>');
html.push('<span>民族名称:'+name+'</span>');
html.push('<br/>');
}
//如果拼装后的html数组元素>0,添加到面面上。
html.length>0&&nationNameContent.append(html.join(''));
//html.length>0&&(jsNationNameContent.innerHTML=html.join(''));
D. jquery each如何遍历出这样的json到页面
alert($(this).nidString);或者alert(list[i].nidString);有试过吗?
E. Jquery中,如何固定一种遍历json的顺序
这和各浏览器的Map键名的遍历方法相关,jquery只不过是包装了一下 for (key in obj)。
解决方法为将键名放入的专数组属,通过遍历数组的方式就不会有问题了。
var a = [];
$.each(obj, function(key, val) { a[a.length] = key; });
a.sort();
$.each(a, function(i, key) {
window.alert("key = " + obj[key]); // 访问JSON对象属性
});
F. jquery解析json怎么解析
获取JSON数据,在jQuery中有一个简单的方法 $.getJSON() 可以实现。
下面引用的是官方API对$.getJSON()的说明:
jQuery.getJSON( url, [data,] [success(data, textStatus, jqXHR)] )
urlA string containing the URL to which the request is sent.
dataA map or string that is sent to the server with the request.
success(data, textStatus, jqXHR)A callback function that is executed if the request succeeds.
回调函数中接受三个参数,第一个书返回的数据,第二个是状态,第三个是jQuery的XMLHttpRequest,我们只使用到第一个参数。
$.each()是用来在回调函数中解析JSON数据的方法,下面是官方文档:
jQuery.each( collection, callback(indexInArray, valueOfElement) )
collectionThe object or array to iterate over.
callback(indexInArray, valueOfElement)The function that will be executed on every object.
$.each()方法接受两个参数,第一个是需要遍历的对象集合(JSON对象集合),第二个是用来遍历的方法,这个方法又接受两个参数,第一个是遍历的index,第二个是当前遍历的值。哈哈,有了$.each()方法JSON的解析就迎刃而解咯。
functionloadInfo(){
$.getJSON("loadInfo",function(data){
$("#info").html("");//清空info内容
$.each(data.comments,function(i,item){
$("#info").append("<div>"+item.id+"</div>"+"<div>"+item.nickname+"</div>"+
"<div>"+item.content+"</div><hr/>");
});
});
}
G. jquery怎么遍历json数组
看你json串的格式。比如{"id":"1","name":"张三"},{"id":"2","name":"李四"}
这种接近于数组,遍历方式可以for(var i = 0; i = list.size(); i ++)
也可以用内for( var a in list)的方式来遍历,方法容很多就不一一解释了。
也不一定非要用jQuery遍历,jQuery遍历有.each方法,但是需要将你json串解析。
H. jquery each遍历json插入数组的问题
//bysleest2016/05/10
varres='{"list":[{"id":3010,"name":"青海","cityList":[{"id":3011,"name":"西宁"},{"id":3012,"name":"城东区"},{"id":3013,"name":"城西区"}]}]}';
vardataObj=JSON.parse(res);
varlistData=dataObj.list;
varcityAry=[];
for(vari=0;i<listData.length;i++){
vartmp=listData[i];
varcity={
value:tmp.id,
text:tmp.name,
children:[]
}
for(varj=0;j<tmp.cityList.length;j++){
city.children.push({
value:tmp.cityList[j].id,
text:tmp.cityList[j].name
})
}
cityAry.push(city);
}
console.table(cityAry);