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.        }  
  • 相关阅读:
    python 根据数组生成图片
    c++ 字符串转数字
    python 迷宫问题
    JavaScript 判断是否为空
    JavaScript 字符串转数字(整数,浮点数,进制转换)
    c++ 珊格迷宫问题
    python eval的用法
    python pillow 处理图片
    c 结构体
    python pillow 绘制图片
  • 原文地址:https://www.cnblogs.com/coolsundy/p/4204638.html
Copyright © 2011-2022 走看看