您的位置:首页 > Web前端 > jquery > 正文

jquery遍历json字符串

更多 时间:2016-3-31 类别:Web前端 浏览量:1628

jquery遍历json字符串

jquery遍历json字符串

一、对javascript对象的遍历

  •  
  • 
    var json = [
        {"id":"1","tagName":"apple"},
        {"id":"2","tagName":"orange"},
        {"id":"3","tagName":"banana"},
        {"id":"4","tagName":"watermelon"},
        {"id":"5","tagName":"pineapple"}
    ];
    
    $.each(json, function(idx, obj) {
        alert(obj.tagName);
    });
    
    		
  •  

    二、对JSON字符串的遍历

  •  
  • 
    var json = '[{"id":"1","tagName":"apple"},{"id":"2","tagName":"orange"},
    {"id":"3","tagName":"banana"},{"id":"4","tagName":"watermelon"},
    {"id":"5","tagName":"pineapple"}]';
        
    $.each(JSON.parse(json), function(idx, obj) {
        alert(obj.tagName);
    });
    
    //or 
    
    $.each($.parseJSON(json), function(idx, obj) {
        alert(obj.tagName);
    });
    
    		
  •  

    标签:jquery