how to loop through json array in jquery?

前端 未结 8 556
温柔的废话
温柔的废话 2020-12-09 09:03

I have a php page from which I get response in json:

[{\'com\':\'something\'},{\'com\':\'some other thing\'}]

I want to loop it and append

相关标签:
8条回答
  • 2020-12-09 09:43

    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);     
      });
    });
    
    0 讨论(0)
  • 2020-12-09 09:48
    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
    }); 
    
    0 讨论(0)
提交回复
热议问题