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.        }  
  • 相关阅读:
    基于selenium的web自动化框架
    spring boot整合Mybatis
    Spring boot进阶-配置Controller、interceptor...
    spring boot 第一个Demo
    Spring事务管理
    mybatis、Spring整合(eclipse)以及事务管理
    highcharts 根据表格转化为不同的图表
    highChartTable 切换
    highcharts 切换
    九度 1533 最长上升子序列
  • 原文地址:https://www.cnblogs.com/coolsundy/p/4204638.html
Copyright © 2011-2022 走看看