欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

获取JSON中的直接获取或遍历

程序员文章站 2024-01-14 21:25:04
...

基本格式

 var person = {"name":"张三",age:23,"gender":true}
    for{var key in person}//遍历
     {
        alert(key+":"+person[key]);//获取键和值 key为字符串格式
     }

数组格式

   var ps = [{"name":"张三","age":23,"gender":true},{"name":"李四","age":24,"gender":true}
   ,{"name":"王五","age":25,"gender":false}]
   for{var i=0; i<ps.length:i++}//遍历
   {
      var p = ps[i];
     for(var key in p)
     {
        alert(key+":"+p[key]);
     }
   }

嵌套格式

var persons = {"persons":
                    [{"name":"张三","age":23,"gender":true},
                     {"name":"李四","age":24,"gender":true},
                     {"name":"王五","age":25,"gender":false}]};
					//获取王五的值
					var name = persons.persons[2].name

相关标签: JSON