首页 > 代码库 > Javascript中JSON对象的操作以及遍历key/value

Javascript中JSON对象的操作以及遍历key/value

//遍历获取值:function text(){    var json = {"options":"[{/"text/":/"王家湾/",/"value/":/"9/"},{/"text/":/"李家湾/",/"value/":/"10/"},{/"text/":/"邵家湾/",/"value/":/"13/"}]"}     json = eval(json.options)    for(var i=0; i<json.length; i++)    {       alert(json[i].text+" " + json[i].value)    }  }  var data=http://www.mamicode.com/[{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 );  }  
//获取key/valuevar obj = {};for(var k in obj) {    //遍历对象,k即为key,obj[k]为当前k对应的值    console.log(k);    console.log(obj[k]); }

 

Javascript中JSON对象的操作以及遍历key/value