Web前端编程-JavaScript。本文来源于90建站自学网:http://www.dongyingit.cn 1. [代码] [JavaScript]代码 //设置一个json字符串变量并赋值var jsonstr = '{"employees":[' +'{"firstName":"Bill","lastName":"Gates" },' +'
1. [代码][JavaScript]代码
//设置一个json字符串变量并赋值 var jsonstr = '{"employees":[' + '{"firstName":"Bill","lastName":"Gates" },' + '{"firstName":"George","lastName":"Bush" },' + '{"firstName":"Thomas","lastName":"Carter" }],"sdf":"sdf","山东":"济南","北京":"故宫"}'; //设置json字符串转化为json对象 var jsonobj = eval("("+jsonstr+")"); //循环遍历json对象所有数据 for(var p in jsonobj)//第一层循环 { if(typeof(jsonobj[p])=="object"){//如果json值还是一个object,则继续第二层循环 var jsonobj2 = jsonobj[p]; for(var k in jsonobj2){ alert("firstname:"+k+jsonobj2[k].firstName); alert("lastName:"+k+jsonobj2[k].lastName); } }else{//如果是是字符串不是对象则直接输出 alert(jsonobj[p]); } }