看到网上有很多朋友问遍历json数组的代码,特为大家整理了一些,需要的朋友可以看下。

<script type="text/javascript"> 
var d1 =[{"text":"王家湾","value":"9"},{"text":"李家湾","value":"10"},{"text":"邵家湾","value":"13"}]; 
$(d1).each(function(){ 
alert(this.text+" "+this.value); 
}); 
</script> 

不用JQuery

<script type="text/javascript"> 
var json = {"options":"[{\"text\":\"王家湾\",\"value\":\"9\"},{\"text\":\"李家湾\",\"value\":\"10\"},{\"text\":\"邵家湾\",\"value\":\"13\"}]"} 
json = eval(json.options) 
for(var i=0; i<json.length; i++) 
{ 
alert(json[i].text+" " + json[i].value) 
} 
</script> 

js遍历json数组

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<HEAD> 
<TITLE> New Document </TITLE> 
<META NAME="Generator" CONTENT="EditPlus"> 
<META NAME="Author" CONTENT=""> 
<META NAME="Keywords" CONTENT=""> 
<META NAME="Description" CONTENT=""> 
<script language="javascript"> 
function e() 
{ 
var json=[{"kl_id":"2","kl_title":"Test date","kl_content":"Test date","kl_type":"1","id":"1"},{"kl_id":"2","kl_title":"Test","kl_content":"Test","kl_type":"1","id":"2"}] 
var str = ""; 
for (var one in json) 
{ 
for(var key in json[one]) 
{ 
str += json[one][key] + ","; 
} 
} 
document.form.txtName.value = str; 
} 
</script> 
</HEAD> 

<BODY> 
<form name="form"> 
<a href="#" onclick="e();">Click</a> 
<br> 
<input name="txtName" value="" type="text" size="50"> 
</form> 
</BODY> 
</HTML> 
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com