博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

JS 循环遍历JSON数据

Posted on 2012-12-07 14:24  love楠  阅读(2960)  评论(0编辑  收藏  举报

JS 循环遍历JSON数据

JSON数据如:{"options":"[{\"text\":\"王家湾\",\"value\":\"9\"},{\"text\":\"李家湾\",\"valu
e\":\"10\"},{\"text\":\"邵家湾\",\"value\":\"13\"}]"}

用js可以写成: JSON数据如:{"options":"[{\"text\":\"王家湾\",\"value\":\"9\"},{\"text\":\"李家湾\",\"valu
e\":\"10\"},{\"text\":\"邵家湾\",\"value\":\"13\"}]"}

用js可以写成:

view plaincopy to clipboardprint?
01.var data=[{name:"a",age:12},{name:"b",age:11},{name:"c",age:13},{name:"d",age:14}];  
02.      for(var o in data){  
03.        alert(o);  
04.        alert(data[o]);  
05.        alert("text:"+data[o].name+" value:"+data[o].age );  
06.      } 
var data=[{name:"a",age:12},{name:"b",age:11},{name:"c",age:13},{name:"d",age:14}];
   for(var o in data){
    alert(o);
  alert(data[o]);
    alert("text:"+data[o].name+" value:"+data[o].age );
   }

或是

view plaincopy to clipboardprint?
01.<script type="text/javascript">  
02.function text(){  
03.  var json = {"options":"[{\"text\":\"王家湾\",\"value\":\"9\"},{\"text\":\"李家湾\",\"value\":\"10\"},{\"text\":\"邵家湾\",\"value\":\"13\"}]"}   
04.  json = eval(json.options)  
05.  for(var i=0; i<json.length; i++)  
06.  {  
07.     alert(json[i].text+" " + json[i].value)  
08.  }  
09.}  
10.</script> 

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/songylwq/archive/2010/12/01/6047609.aspx

view plaincopy to clipboardprint?
01.var data=[{name:"a",age:12},{name:"b",age:11},{name:"c",age:13},{name:"d",age:14}];  
02.      for(var o in data){  
03.        alert(o);  
04.        alert(data[o]);  
05.        alert("text:"+data[o].name+" value:"+data[o].age );  
06.      } 
var data=[{name:"a",age:12},{name:"b",age:11},{name:"c",age:13},{name:"d",age:14}];
  for(var o in data){
  alert(o);
alert(data[o]);
  alert("text:"+data[o].name+" value:"+data[o].age );
  }

或是

view plaincopy to clipboardprint?
01.<script type="text/javascript">  
02.function text(){  
03.  var json = {"options":"[{\"text\":\"王家湾\",\"value\":\"9\"},{\"text\":\"李家湾\",\"value\":\"10\"},{\"text\":\"邵家湾\",\"value\":\"13\"}]"}   
04.  json = eval(json.options)  
05.  for(var i=0; i<json.length; i++)  
06.  {  
07.     alert(json[i].text+" " + json[i].value)  
08.  }  
09.}  
10.</script> 

 

    window.onload = function () {
        var jsJosn1 = '<%=Jsonstr%>';   //从后台获取json商品字符串
        var obj = eval('(' + jsJosn1 + ')');  //将字符串转为json对象
        obj = eval(obj.TableInfo);
        //循环商品
        for (var i = 0; i < obj.length; i++) {
            //alert(obj[i].BaseProductName + " " + obj[i].BaseProductNO + " " + obj[i].OrderPrice + " " + obj[i].OrderAmount + " " + obj[i].Unit + " " + obj[i].BPID);
            AddProduct(obj[i].BaseProductName, obj[i].BaseProductNO, obj[i].OrderPrice, obj[i].Unit, obj[i].OrderAmount, obj[i].BPID);
        }
    }