2015-04-28 3 views
0

Я делаю школьный проект, и я работаю с json формой вывода Google API геокодирования.Как получить определенную информацию от выхода Json

Я не знаю, как сохранить «formatted_address» (см. Ниже) вывода в переменной, чтобы позже использовать эту информацию.

Вот часть вывода json, с которым я работаю.

"results" : [ 
    { 
    "address_components" : [ 
     { 
      "long_name" : "11A", 
      "short_name" : "11A", 
      "types" : [ "street_number" ] 
     }, 
     { 
      "long_name" : "Albyn Place", 
      "short_name" : "Albyn Pl", 
      "types" : [ "route" ] 
     }, 
     { 
      "long_name" : "Aberdeen", 
      "short_name" : "Aberdeen", 
      "types" : [ "locality", "political" ] 
     }, 
     { 
      "long_name" : "Aberdeen", 
      "short_name" : "Aberdeen", 
      "types" : [ "postal_town" ] 
     }, 
     { 
      "long_name" : "Aberdeen City", 
      "short_name" : "Aberdeen City", 
      "types" : [ "administrative_area_level_2", "political" ] 
     }, 
     { 
      "long_name" : "United Kingdom", 
      "short_name" : "GB", 
      "types" : [ "country", "political" ] 
     }, 
     { 
      "long_name" : "AB10 1YE", 
      "short_name" : "AB10 1YE", 
      "types" : [ "postal_code" ] 
     } 
    ], 
    "formatted_address" : "11A Albyn Place, Aberdeen, Aberdeen City AB10 1YE, UK", 
    "geometry" : { 
     "location" : { 
      "lat" : 57.1425663, 
      "lng" : -2.1144957 
     }, 
     "location_type" : "ROOFTOP", 
     "viewport" : { 
      "northeast" : { 
       "lat" : 57.1439152802915, 
       "lng" : -2.113146719708498 
      }, 
      "southwest" : { 
       "lat" : 57.1412173197085, 
       "lng" : -2.115844680291502 
      } 
     } 
    }, 
    "place_id" : "ChIJ89KpnycOhEgR96EdolCIM8U", 
    "types" : [ "street_address" ] 
    }, 
    { 
    "address_components" : [ 
     { 
      "long_name" : "Union Grove (o/s 82)", 
      "short_name" : "Union Grove (o/s 82)", 
      "types" : [ "bus_station", "transit_station", "establishment" ] 
     }, 
     { 
      "long_name" : "Aberdeen", 
      "short_name" : "Aberdeen", 
      "types" : [ "locality", "political" ] 
     }, 
     { 
      "long_name" : "Aberdeen", 
      "short_name" : "Aberdeen", 
      "types" : [ "postal_town" ] 
     }, 
     { 
      "long_name" : "Aberdeen City", 
      "short_name" : "Aberdeen City", 
      "types" : [ "administrative_area_level_2", "political" ] 
     }, 
     { 
      "long_name" : "United Kingdom", 
      "short_name" : "GB", 
      "types" : [ "country", "political" ] 
     }, 
     { 
      "long_name" : "AB10", 
      "short_name" : "AB10", 
      "types" : [ "postal_code_prefix", "postal_code" ] 
     } 
    ], 
    "formatted_address" : "Union Grove (o/s 82), Aberdeen, Aberdeen, Aberdeen City AB10, UK", 
    "geometry" : { 
     "location" : { 
      "lat" : 57.141483, 
      "lng" : -2.115739 
     }, 
     "location_type" : "APPROXIMATE", 
     "viewport" : { 
      "northeast" : { 
       "lat" : 57.1428319802915, 
       "lng" : -2.114390019708498 
      }, 
      "southwest" : { 
       "lat" : 57.1401340197085, 
       "lng" : -2.117087980291502 
      } 
     } 
    }, 
    "place_id" : "ChIJra8KiycOhEgRSR-z6DmOvO8", 
    "types" : [ "bus_station", "transit_station", "establishment" ] 
    }, 
    { 
    "address_components" : [ 
     { 
      "long_name" : "AB10 1YE", 
      "short_name" : "AB10 1YE", 
      "types" : [ "postal_code" ] 
     }, 
     { 
      "long_name" : "Aberdeen", 
      "short_name" : "Aberdeen", 
      "types" : [ "locality", "political" ] 
     }, 
     { 
      "long_name" : "Aberdeen", 
      "short_name" : "Aberdeen", 
      "types" : [ "postal_town" ] 
     }, 
     { 
      "long_name" : "Aberdeen City", 
      "short_name" : "Aberdeen City", 
      "types" : [ "administrative_area_level_2", "political" ] 
     }, 
     { 
      "long_name" : "United Kingdom", 
      "short_name" : "GB", 
      "types" : [ "country", "political" ] 
     } 
    ], 
    "formatted_address" : "Aberdeen, Aberdeen, Aberdeen City AB10 1YE, UK", 
    "geometry" : { 
     "bounds" : { 
      "northeast" : { 
       "lat" : 57.1429049, 
       "lng" : -2.1140542 
      }, 
      "southwest" : { 
       "lat" : 57.141979, 
       "lng" : -2.1148766 
      } 
     }, 
     "location" : { 
      "lat" : 57.1425663, 
      "lng" : -2.1144957 
     }, 
     "location_type" : "APPROXIMATE", 
     "viewport" : { 
      "northeast" : { 
       "lat" : 57.14379093029149, 
       "lng" : -2.113116419708498 
      }, 
      "southwest" : { 
       "lat" : 57.1410929697085, 
       "lng" : -2.115814380291502 
      } 
     } 
    }, 
    "place_id" : "ChIJWey5nycOhEgRXR9pzwov_pY", 
    "types" : [ "postal_code" ] 
    }, 
    { 
    "address_components" : [ 
     { 
      "long_name" : "AB10", 
      "short_name" : "AB10", 
      "types" : [ "postal_code_prefix", "postal_code" ] 
     }, 
     { 
      "long_name" : "Aberdeen", 
      "short_name" : "Aberdeen", 
      "types" : [ "postal_town" ] 
     }, 
     { 
      "long_name" : "United Kingdom", 
      "short_name" : "GB", 
      "types" : [ "country", "political" ] 
     } 
    ], 
    "formatted_address" : "Aberdeen AB10, UK", 
    "geometry" : { 
     "bounds" : { 
      "northeast" : { 
       "lat" : 57.1511959, 
       "lng" : -2.0903783 
      }, 
      "southwest" : { 
       "lat" : 57.1094073, 
       "lng" : -2.1530504 
      } 
     }, 
     "location" : { 
      "lat" : 57.131255, 
      "lng" : -2.1264712 
     }, 
     "location_type" : "APPROXIMATE", 
     "viewport" : { 
      "northeast" : { 
       "lat" : 57.1511959, 
       "lng" : -2.0903783 
      }, 
      "southwest" : { 
       "lat" : 57.1094073, 
       "lng" : -2.1530504 
      } 
     } 
    }, 
    "place_id" : "ChIJOT6RmCAOhEgRRCBB7_j2M30", 
    "types" : [ "postal_code_prefix", "postal_code" ] 
    }, 
    { 
    "address_components" : [ 
     { 
      "long_name" : "Aberdeen", 
      "short_name" : "Aberdeen", 
      "types" : [ "locality", "political" ] 
     }, 
     { 
      "long_name" : "Aberdeen City", 
      "short_name" : "Aberdeen City", 
      "types" : [ "administrative_area_level_2", "political" ] 
     }, 
     { 
      "long_name" : "Scotland", 
      "short_name" : "Scotland", 
      "types" : [ "administrative_area_level_1", "political" ] 
     }, 
     { 
      "long_name" : "United Kingdom", 
      "short_name" : "GB", 
      "types" : [ "country", "political" ] 
     } 
    ], 
    "formatted_address" : "Aberdeen, Aberdeen City, UK", 
    "geometry" : { 
     "bounds" : { 
      "northeast" : { 
       "lat" : 57.19565069999999, 
       "lng" : -2.0461811 
      }, 
      "southwest" : { 
       "lat" : 57.1041518, 
       "lng" : -2.2058926 
      } 
     }, 
     "location" : { 
      "lat" : 57.149717, 
      "lng" : -2.094278 
     }, 
     "location_type" : "APPROXIMATE", 
     "viewport" : { 
      "northeast" : { 
       "lat" : 57.19565069999999, 
       "lng" : -2.0461811 
      }, 
      "southwest" : { 
       "lat" : 57.1041518, 
       "lng" : -2.2058926 
      } 
     } 
    }, 
    "place_id" : "ChIJSXXXH0wFhEgRcsT0XNoFu-g", 
    "types" : [ "locality", "political" ] 
    }, 
    { 
    "address_components" : [ 
     { 
      "long_name" : "Aberdeen City", 
      "short_name" : "Aberdeen City", 
      "types" : [ "administrative_area_level_2", "political" ] 
     }, 
     { 
      "long_name" : "Scotland", 
      "short_name" : "Scotland", 
      "types" : [ "administrative_area_level_1", "political" ] 
     }, 
     { 
      "long_name" : "United Kingdom", 
      "short_name" : "GB", 
      "types" : [ "country", "political" ] 
     } 
    ], 
    "formatted_address" : "Aberdeen City, UK", 
    "geometry" : { 
     "bounds" : { 
      "northeast" : { 
       "lat" : 57.2353521, 
       "lng" : -2.0461828 
      }, 
      "southwest" : { 
       "lat" : 57.07618890000001, 
       "lng" : -2.3609398 
      } 
     }, 
     "location" : { 
      "lat" : 57.1506564, 
      "lng" : -2.1467599 
     }, 
     "location_type" : "APPROXIMATE", 
     "viewport" : { 
      "northeast" : { 
       "lat" : 57.2353521, 
       "lng" : -2.0461828 
      }, 
      "southwest" : { 
       "lat" : 57.07618890000001, 
       "lng" : -2.3609398 
      } 
     } 
    }, 
    "place_id" : "ChIJs6qVe2kRhEgRkLBCBhpoDAM", 
    "types" : [ "administrative_area_level_2", "political" ] 
    }, 
    { 
    "address_components" : [ 
     { 
      "long_name" : "Aberdeen", 
      "short_name" : "Aberdeen", 
      "types" : [ "postal_town" ] 
     }, 
     { 
      "long_name" : "United Kingdom", 
      "short_name" : "GB", 
      "types" : [ "country", "political" ] 
     } 
    ], 
    "formatted_address" : "Aberdeen, UK", 
    "geometry" : { 
     "bounds" : { 
      "northeast" : { 
       "lat" : 57.3274181, 
       "lng" : -2.0076331 
      }, 
      "southwest" : { 
       "lat" : 57.0411197, 
       "lng" : -2.3365801 
      } 
     }, 
     "location" : { 
      "lat" : 57.2177758, 
      "lng" : -2.1823124 
     }, 
     "location_type" : "APPROXIMATE", 
     "viewport" : { 
      "northeast" : { 
       "lat" : 57.3274181, 
       "lng" : -2.0076331 
      }, 
      "southwest" : { 
       "lat" : 57.0411197, 
       "lng" : -2.3365801 
      } 
     } 
    }, 
    "place_id" : "ChIJe57LATIShEgRdUZWMa_i0ag", 
    "types" : [ "postal_town" ] 
    }, 
    { 
    "address_components" : [ 
     { 
      "long_name" : "Scotland", 
      "short_name" : "Scotland", 
      "types" : [ "administrative_area_level_1", "political" ] 
     }, 
     { 
      "long_name" : "United Kingdom", 
      "short_name" : "GB", 
      "types" : [ "country", "political" ] 
     } 
    ], 
    "formatted_address" : "Scotland, UK", 
    "geometry" : { 
     "bounds" : { 
      "northeast" : { 
       "lat" : 60.8607515, 
       "lng" : -0.7246751 
      }, 
      "southwest" : { 
       "lat" : 54.6332381, 
       "lng" : -8.6498565 
      } 
     }, 
     "location" : { 
      "lat" : 56.49067119999999, 
      "lng" : -4.2026458 
     }, 
     "location_type" : "APPROXIMATE", 
     "viewport" : { 
      "northeast" : { 
       "lat" : 60.84567610000001, 
       "lng" : -0.7247473999999999 
      }, 
      "southwest" : { 
       "lat" : 54.6332381, 
       "lng" : -8.6492466 
      } 
     } 
    }, 
    "place_id" : "ChIJn6HyA8TiYUgRFAfDCdj6wec", 
    "types" : [ "administrative_area_level_1", "political" ] 
    }, 
    { 
    "address_components" : [ 
     { 
      "long_name" : "United Kingdom", 
      "short_name" : "GB", 
      "types" : [ "country", "political" ] 
     } 
    ], 
    "formatted_address" : "United Kingdom", 
    "geometry" : { 
     "bounds" : { 
      "northeast" : { 
       "lat" : 60.8606697, 
       "lng" : 33.916555 
      }, 
      "southwest" : { 
       "lat" : 34.5626034, 
       "lng" : -8.649357199999999 
      } 
     }, 
     "location" : { 
      "lat" : 55.378051, 
      "lng" : -3.435973 
     }, 
     "location_type" : "APPROXIMATE", 
     "viewport" : { 
      "northeast" : { 
       "lat" : 60.856553, 
       "lng" : 1.7627096 
      }, 
      "southwest" : { 
       "lat" : 49.8669688, 
       "lng" : -8.649357199999999 
      } 
     } 
    }, 
    "place_id" : "ChIJqZHHQhE7WgIReiWIMkOg-MQ", 
    "types" : [ "country", "political" ] 
    } 
], 
"status" : "OK" 

До сих пор я пытаюсь это в моем файле JS:

var restourantAddressRequest = "https://maps.googleapis.com/maps/api/geocode/json?latlng="+restLatitude+","+restLongtitude+"&key=API_KEY; 
//alert(restourantAddressRequest.results.formatted_address); 
$("#restaurant_address").html("Address: " + restourantAddressRequest.results.formatted_address); 
+1

Если добавить больше деталей к вашему вопросу, я я уверен, что многие смогут помочь. Как вы храните данные? MySql, текст, память браузера, сеанс ... brail? – HappyCoder

+0

Спасибо за ваш ответ. Я добавлю больше деталей к вопросу. –

+2

Пожалуйста, добавьте фактический вывод JSON, а не экран. –

ответ

1

Вы должны Разбирает ваш ответ JSON перед доступом к ней:

var restourantAddressRequest = "https://maps.googleapis.com/maps/api/geocode/json?latlng="+restLatitude+","+restLongtitude+"&key=API_KEY"; 
var json= JSON.parse(restourantAddressRequest); 
//alert(restourantAddressRequest.results.formatted_address); 
$("#restaurant_address").html("Address: " + restourantAddressRequest.results.formatted_address); 

Надеется, что это будет работать.

+0

кажется, что это сделал трюк. Благодаря! :) –

2

Вы хотите использовать точечную нотацию, чтобы получить нужное значение. Google V3 Geocoder возвращает массив таким образом, что вы ищете заключается в следующем:

results[0].formatted_address 

Вот скрипка, которая предупреждает форматированный адрес для вас:

http://jsfiddle.net/iamjpg/h39041w2/

Я надеюсь, что это полезно.

+0

Ничего себе. Спасибо за это! Это было проще, чем я думал :) –

+0

Когда я попробовал этот ресторанAddressRequest.results [0] .formatted_address, он не работал: / –

0

я решить эту проблему с помощью getJSON метода в JQuery, как это:

var restaurantAddressRequest = "https://maps.googleapis.com/maps/api/geocode/json?latlng="+restLatitude+","+restLongtitude+"&key=API_KEY"; 
//alert(restourantAddressRequest.results.formatted_address); 

$.getJSON(restaurantAddressRequest,function(jsondata){ 
    $("#restaurant_address").html("Address: " + jsondata.results[0].jsondata.results[0].formatted_address); 
}); 
1

Если вы хотите сделать это асинхронно:

//Show that you are loading the data 

$("#restaurant_address").html("Address: Loading... ");  

//Set the url: 

var url = 'https://maps.googleapis.com/maps/api/geocode/json?latlng="+restLatitude+","+restLongtitude+"&key=API_KEY"'; 

//Make the Ajax call: 


     $.ajax({ 
      type: 'GET', 
      async: true, 
      url: url, 
      dataType: "json", 

      success: function(responseObject){ 


       if (responseObject.success) 
       { 
        $("#restaurant_address").html("Address: " + ...); 

       } else { 
        $("#restaurant_address").html("Address: n/a"); 
       } 

      } 
     }); 
Смежные вопросы