jquery loop on Json data using $.each

前端 未结 4 1985
后悔当初
后悔当初 2020-11-22 14:56

I have the following JSON returned in a variable called data.

THIS IS THE JSON THAT GETS RETURNED...

[ 
{\"Id\": 10004, \"PageName\"         


        
4条回答
  •  情深已故
    2020-11-22 15:40

    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);
    });
    

    these two options work well, unless you have something like:

    var data.result = [ 
     {"Id": 10004, "PageName": "club"}, 
     {"Id": 10040, "PageName": "qaz"}, 
     {"Id": 10059, "PageName": "jjjjjjj"}
    ];
    
    $.each(data.result, function(i, item) {
        alert(data.result[i].PageName);
    });
    

    EDIT:

    try with this and describes what the result

    $.get('/Cms/GetPages/123', function(data) {
      alert(data);
    });
    

    FOR EDIT 3:

    this corrects the problem, but not the idea to use "eval", you should see how are the response in '/Cms/GetPages/123'.

    $.get('/Cms/GetPages/123', function(data) {
      $.each(eval(data.replace(/[\r\n]/, "")), function(i, item) {
       alert(item.PageName);
      });
    });
    

提交回复
热议问题