js遍历json对象(推荐)

JavaScript021

js遍历json对象(推荐),第1张

原生js遍历json对象

遍历json对象:

无规律:

有规律:

遍历json对象

有如下 json对象:

var obj ={"name":"冯娟","password":"123456","department":"技术部","sex":"女","old":30}

遍历方法:

数组对象的遍历:

下面我们对照一下js和jquer的遍历方法:

原文网址: https://www.cnblogs.com/qingqingzou-143/p/7079725.html

看这样一段函数定义的代码就明白了function json_to_format_string(obj,deep){ var html = "" var blank_string = "" for (var i = 0i <deepi++) { blank_string += " " } for(var name in obj){ if (typeof(obj[name])=="object") { html += blank_string + name +": "+ "

" html += json_to_format_string(obj[name],deep+1) }else{ html += blank_string + name +": "+ obj[name]+"

" } } return html}

用  for  in  循环 遍历

var str = '[{"name":"宗2瓜","num":"1","price":"122"},{"name":"宗呱呱","num":"1","price":"100"}]'

var xqo = eval('(' + str + ')')

for(var i in xqo){

alert(xqo[i].name)

}