jQuery 如何在jquery中循环json数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20772417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to loop through json array in jquery?
提问by Niket Malik
I have a php page from which I get response in json:
我有一个 php 页面,我从中得到了 json 响应:
[{'com':'something'},{'com':'some other thing'}]
I want to loop it and append each to a div.
我想循环它并将每个附加到一个 div。
This is what I tried:
这是我尝试过的:
var obj = jQuery.parseJSON(response);
$.each(obj.com, function(key,value) {
alert(key+':'+value);
}
This alerts as undefined
, and also response is the json array..
这警告为undefined
,并且响应也是 json 数组..
Please help.
请帮忙。
回答by Dell
Your array has default keys(0,1) which store object {'com':'some thing'}
use:
您的数组具有存储对象{'com':'some thing'}
使用的默认键(0,1):
var obj = jQuery.parseJSON(response);
$.each(obj, function(key,value) {
alert(value.com);
});
回答by Prateek
Try this:
尝试这个:
var data = jQuery.parseJSON(response);
$.each(data, function(key, item)
{
console.log(item.com);
});
or
或者
var data = $.parseJSON(response);
$(data).each(function(i,val)
{
$.each(val,function(key,val)
{
console.log(key + " : " + val);
});
});
回答by Pasupathi Thangavel
var data = [
{"Id": 10004, "PageName": "club"},
{"Id": 10040, "PageName": "qaz"},
{"Id": 10059, "PageName": "jjjjjjj"}
];
$.each(data, function(i, item) {
alert(data[i].PageName);
});?
$.each(data, function(i, item) {
alert(item.PageName);
});?
Or else You can try this method
或者你可以试试这个方法
var data = jQuery.parseJSON(response);
$.each(data, function(key,value) {
alert(value.Id); //It will shows the Id values
});
回答by undefined
You are iterating through an undefined
value, ie, com
property of the Array's object, you should iterate through the array itself:
您正在遍历一个undefined
值,即com
数组对象的属性,您应该遍历数组本身:
$.each(obj, function(key,value) {
// here `value` refers to the objects
});
Also note that jQuery intelligently tries to parse the sent JSON, probably you don't need to parse the response. If you are using $.ajax()
, you can set the dataType
to json
which tells jQuery parse the JSON for you.
另请注意,jQuery 会智能地尝试解析发送的 JSON,您可能不需要解析响应。如果您正在使用$.ajax()
,您可以将 设置dataType
为json
告诉 jQuery 为您解析 JSON。
If it still doesn't work, check the browser's console for troubleshooting.
如果它仍然不起作用,请检查浏览器的控制台以进行故障排除。
回答by undefined
var data=[{'com':'something'},{'com':'some other thing'}];
$.each(data, function() {
$.each(this, function(key, val){
alert(val);//here data
alert (key); //here key
});
});
回答by undefined
you can get the key value pair as
你可以得到键值对作为
<pre>
function test(){
var data=[{'com':'something'},{'com':'some other thing'}];
$.each(data, function(key,value) {
alert(key);
alert(value.com);
});
}
</pre>
回答by Vaibhav Jain
Try this:
尝试这个:
for(var i = 0; i < data.length; i++){
console.log(data[i].com)
}
回答by McNiel Viray
try this
尝试这个
var events = [];
alert(doc);
var obj = jQuery.parseJSON(doc);
$.each(obj, function (key, value) {
alert(value.title);
});
});