zoukankan      html  css  js  c++  java
  • 遍历josn的三种方式

    第一种:使用for循环

    js代码:

    [javascript] view plaincopy
     
    1. function CyclingJson1() {  
    2.             var testJson = '[{ "name": "小强", "age": 16 },{"name":"小明","age":17}]';  
    3.             testJson = eval("(" + testJson + ")");  
    4.             for (var i = 0; i < testJson.length; i++) {  
    5.                 alert(testJson[i].name);  
    6.             }  
    7.         }  

    第二种:使用关键字in

    js代码:

    [c-sharp] view plaincopy
     
    1. function CyclingJson2() {  
    2.            var testJson = '[{ "name": "小强", "age": 16 },{"name":"小明","age":17}]';  
    3.            testJson = eval("(" + testJson + ")");  
    4.            for (var i in testJson) {  
    5.                alert(testJson[i].name);  
    6.            }  
    7.        }  

    第三种;使用jquery.each()函数

    [javascript] view plaincopy
     
    1. function CyclingJson3() {  
    2.            var testJson = '[{ "name": "小强", "age": 16 },{"name":"小明","age":17}]';  
    3.            testJson = eval("(" + testJson + ")");  
    4.            $.each(testJson, function (i, n) {  
    5.                alert(i); //i为索引值   
    6.                alert(n.name); //n为遍历的值  
    7.            });  
    8.        }  
  • 相关阅读:
    poj2186强连通分量
    poj1459SAP最大流模板题
    poj2391Floyd+二分+最大流
    curl上传下载入门
    Mysql存储过程
    小球旋转
    钟表单摆
    java小记 摘抄
    servlet的一些收集总结
    Javascript基础小结
  • 原文地址:https://www.cnblogs.com/coolsundy/p/4204638.html
Copyright © 2011-2022 走看看