$.each()遍历Json对象

1.遍历JavaScript数组对象:

 

var json = [ //中括号起始
    {"id":"1","tagName":"apple"},
    {"id":"2","tagName":"orange"},
    {"id":"3","tagName":"banana"}
];
 
$.each(json, function(index, obj) {
    alert(obj.tagName);
});

 

 

2.遍历Json对象:

var json = { //大括号起始
    {"id":"1","tagName":"apple"},
    {"id":"2","tagName":"orange"},
    {"id":"3","tagName":"banana"}
};
 
$.each(json, function(index, obj) {
    alert(obj['tagName']);
});

 

3.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.parse(json), function(idx, obj) {
    alert(obj.tagName);
});
 
//或者
 
$.each($.parseJSON(json), function(idx, obj) {
    alert(obj.tagName);
});

 

posted @ 2015-03-19 17:37  IT浪潮之巅  阅读(276)  评论(0编辑  收藏  举报
   友情链接: 淘宝优惠券