Home > Web Front-end > JS Tutorial > body text

JS method to loop through JSON data_javascript skills

WBOY
Release: 2016-05-16 16:42:19
Original
1364 people have browsed it

JSON data such as: {"options":"[{/"text/":/"Wangjiawan/",/"value/":/"9/"},{/"text/":/" Lijiawan/",/"valu
e/":/"10/"},{/"text/":/"Shaojiawan/",/"value/":/"13/"}]"}

can be written in js as:

var data=[{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 ); 
}
Copy after login

or

<script type="text/javascript"> 
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) 
} 
} 
</script>
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!