我有一个PHP页面,从中我以JSON格式获取响应:
[{'com':'something'},{'com':'some other thing'}]
我想循环并将每个元素添加到一个div中。
这是我尝试的代码:var obj = jQuery.parseJSON(response);
$.each(obj.com, function(key,value) {
alert(key+':'+value);
}
这会触发 undefined
提示,并且响应是 JSON 数组。
我有一个PHP页面,从中我以JSON格式获取响应:
[{'com':'something'},{'com':'some other thing'}]
我想循环并将每个元素添加到一个div中。
这是我尝试的代码:var obj = jQuery.parseJSON(response);
$.each(obj.com, function(key,value) {
alert(key+':'+value);
}
这会触发 undefined
提示,并且响应是 JSON 数组。
您的数组具有默认键(0,1),存储对象{'com':'some thing'}
用法:
var obj = jQuery.parseJSON(response);
$.each(obj, function(key,value) {
alert(value.com);
});
.hide().fadeIn(800);
,但它会一次性淡入整个内容,是否可能逐个淡入? - Niket Malik试试这个:
var data = jQuery.parseJSON(response);
$.each(data, function(key, item)
{
console.log(item.com);
});
或者var data = $.parseJSON(response);
$(data).each(function(i,val)
{
$.each(val,function(key,val)
{
console.log(key + " : " + val);
});
});
[object, object]
我是通过 AJAX 调用获取 JSON 数据的 - Niket Malikvar 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);
});
var data = jQuery.parseJSON(response);
$.each(data, function(key,value) {
alert(value.Id); //It will shows the Id values
});
您正在遍历一个未定义的值,即数组对象的com
属性,您应该遍历数组本身:
$.each(obj, function(key,value) {
// here `value` refers to the objects
});
$.ajax()
,可以将dataType
设置为json
,告诉jQuery为您解析JSON。var data=[{'com':'something'},{'com':'some other thing'}];
$.each(data, function() {
$.each(this, function(key, val){
alert(val);//here data
alert (key); //here key
});
});
你可以获取键值对,如下所示:
<pre>
function test(){
var data=[{'com':'something'},{'com':'some other thing'}];
$.each(data, function(key,value) {
alert(key);
alert(value.com);
});
}
</pre>
试试这个:
for(var i = 0; i < data.length; i++){
console.log(data[i].com)
}
试一下这个
var events = [];
alert(doc);
var obj = jQuery.parseJSON(doc);
$.each(obj, function (key, value) {
alert(value.title);
});