1. json格式-访问Json属性名、属性值
<script> //JSON格式的attr访问 - 点号或者中括号 -注意:中括号里面可以放变量 var imgData={ "name":"图片描述和图片相对路径", "title":"logo图片","url":"logo.png" }; alert( imgData.name ); alert( imgData["name"] ); var str="name" ; alert( imgData[str] ); var json1={"name":"这里是一组图片的文字描述和图片相对路径", "title":["logo图片","图片1","图片2","图片3"], "url":["logo.png","1.png","2.png","3.png"] }; alert( json1.name); alert( json1.url ); alert( json1.url[1] ); alert( json1["name"]); alert( json1["url"] ); alert( json1["url"][1] ); var json2 ={ "name":"图片描述和图片相对路径", "imgData":[ { "name":"图片1", "title":"logo图片1","url":"logo1.png" } , { "name":"图片2", "title":"logo图片2","url":"logo2.png" } , { "name":"图片3", "title":"logo图片3","url":"logo3.png" } ] }; alert( json2.name ); alert( json2.imgData ); alert( json2.imgData[0].name ); alert( json2["name"] ); alert( json2["imgData"] ); alert( json2["imgData"][0].name ); </script>
2.for-in 访问Json属性名称 ,属性值
<script> var json1= { "name":"july", "age":18, "imgSrc":["1.png","2.png","3.png" ] }; /* for-in 访问属性名称: name age imgSrc*/ for( var attr in json1){ console.log( attr); } /* for-in 访问属性值: july 18 ["1.png", "2.png", "3.png"] */ for( var attr in json1){ console.log( json1[attr]); } </script>
3.for-in 访问对象的属性名称 ,属性值
// for-in 访问对象的属性名、属性值 for( var attr in window){ console.log( attr ); } for( var attr in window){ console.log( window[attr] ); }
// for-in 访问对象的属性名、属性值 for( var attr in document){ console.log( attr ); } for( var attr in document){ console.log( document[attr] ); }