如何在jquery中从json数组中提醒每个值

时间:2015-02-04 06:52:04

标签: jquery arrays json

我有这个json数组,如下所示:

[
    {"fname":"name1","id":"1"},
    {"fname":"name2","id":"2"},   
    {"fname":"name3","id":"3"},
    {"fname":"name4","id":"4"}
]

我想提醒每个人的fname。所以会提醒时间。
第一次“name1”,第二次“name2”,第3次“name3”,第4次“name4”。

我怎么能在jquery中这样做?我试过了

$.each(arrofobject, function(index, val) {
    alert(val.category);
});

然而它不起作用

4 个答案:

答案 0 :(得分:1)

var arrofobject = [{"fname":"name1","id":"1"},{"fname":"name2","id":"2"}, {"fname":"name3","id":"3"},{"fname":"name4","id":"4"}];

$.each(arrofobject, function(index, val) {
    alert('Name: '+ val.fname);
    alert('ID: '+val.id);
});

<强> DEMO

答案 1 :(得分:0)

使用forEach()迭代数组

var array = [{
  "fname": "name1",
  "id": "1"
}, {
  "fname": "name2",
  "id": "2"
}, {
  "fname": "name3",
  "id": "3"
}, {
  "fname": "name4",
  "id": "4"
}];

array.forEach(function(v) {
  alert(v.fname);
});

您的代码出现问题:没有category对象属性,因此您需要使用fname

答案 2 :(得分:0)

尝试这种方式:

var arrofobject = [{"fname":"name1","id":"1"},{"fname":"name2","id":"2"},   {"fname":"name3","id":"3"},{"fname":"name4","id":"4"}];

$.each(arrofobject, function(index, val) {
    alert(val.id+" : "+val.fname);
});

答案 3 :(得分:0)

这是jQuery方式,

var arr = [
    {"fname":"name1","id":"1"},
    {"fname":"name2","id":"2"},   
    {"fname":"name3","id":"3"},
    {"fname":"name4","id":"4"}
];

$.each(arr, function(k, v){
     alert(v.id +' ==> '+ v.fname);
})