无法访问json值

时间:2018-09-27 12:09:55

标签: javascript json ajax

我有一个来自ajax响应的JSON数组,但是当我尝试访问它的字段时,它说未定义。 这是我的JSON

[{"material_name":1042,"qty_per_piece":"30","material_qty_req":150},{"material_name":1043,"qty_per_piece":"20","material_qty_req":100},{"material_name":1041,"qty_per_piece":"10","material_qty_req":50}]

我在尝试以下内容

for(var j = 0; j<material.length; j++){
  var matName = material.material_name[j];
  alert(matName);

当我尝试通过它显示未定义时

for(var j = 0; j<material.length; j++){
  var material_names = material[j].material_name;
   alert(material_names);

1 个答案:

答案 0 :(得分:1)

var material = [{"material_name":1042,"qty_per_piece":"30","material_qty_req":150},{"material_name":1043,"qty_per_piece":"20","material_qty_req":100},{"material_name":1041,"qty_per_piece":"10","material_qty_req":50}];

material.forEach(function(v,k){
    console.log(v.material_name);
});