当前位置:Web前端 > jquery> 正文

jquery遍历json字符串

时间:2016-3-31类别:Web前端

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

    标签:
    上一篇下一篇

    猜您喜欢

    热门推荐