Saturday, 31 August 2013

json parse with jquery loop

json parse with jquery loop

hi my code is attached below, though its a part of bigger code
<html>
<head>
<style>
#results{margin-top:100px; width:600px; border:1px solid #000000;
background-color:#CCCCCC; min-height:200px;}
</style>
<script type="text/javascript"
src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script
src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>
<script src="http://code.jquery.com/jquery-latest.min.js"
type="text/javascript"></script>
</head>
<body>
<div id="results"></div>
<script>
var input="Taj Mahal India";
var url = "http://maps.googleapis.com/maps/api/geocode/json?address=" +
input + "&sensor=false";
var check = $.getJSON(url, function (data) {
console.log("inside getjson");
console.log(data);
$.each(data.results.address_components, function(i,inside){
if(inside.types[0]=="route"){
$("#results").append('<p> route : '+this.long_name +'</p>');
}
if(inside.types[0]=="locality"){
$("#results").append('<p> locality : '+this.long_name +'</p>');
}
if(inside.types[0]=="administrative_area_level_2"){
$("#results").append('<p> administrative area 2 : '+this.long_name
+'</p>');
}
if(inside.types[0]=="administrative_area_level_1"){
$("#results").append('<p> administrative area 1 : '+this.long_name
+'</p>');
}
if(inside.types[0]=="country"){
$("#results").append('<p> country : '+this.long_name +'</p>');
}
});
});
</script>
</body>
</html>
The problem is that it doesn't work. I have a strong doubt that i have
done something wrong in $.each(data.results.address_components,
function(i,inside) line.
JSON for Taj Mahal India is here
http://maps.googleapis.com/maps/api/geocode/json?address=Taj%20Mahal%20India&sensor=false

No comments:

Post a Comment