[英]Extract json array data using javascript
I would like to obtain the formatted_address from the json data returned from the following query string by using javascript. 我想通过使用javascript从以下查询字符串返回的json数据中获取formatted_address。
http://maps.googleapis.com/maps/api/geocode/json?latlng=44.4647452,7.3553838&sensor=true http://maps.googleapis.com/maps/api/geocode/json?latlng=44.4647452,7.3553838&sensor=true
function getReverseGeocodingData(lat, lng) {
//use ajax and json
$.ajax({
type: "POST",
url: "http://maps.googleapis.com/maps/api/geocode/json?latlng="+sessionStorage.latitude+","+sessionStorage.longitude+"&sensor=true",
data: jsondata,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var address = response.Result;
var error = response.Error;
if (address != null) {
alert("Found current location: " + address.formatted_address);
sessionStorage.currentAddress = address.formatted_address;
return;
}
},
error: function (msg) {
errorConnection();
}
});
}
I tried getting formatted_address but it return undefined. 我尝试获取formatted_address,但返回未定义。
You can get formatted address like this (Ajax Success) 您可以像这样获得格式化的地址(Ajax成功)
success: function (results) {
var address = (results[0].formatted_address);
alert(JSON.stringify(address));
},
Sorry my previous response only fixed the AJAX request- specifying contentType on a request is not required and has no impact - this is controlled by the server. 抱歉,我以前的响应仅解决了AJAX请求,不需要指定内容类型,也没有影响-这是由服务器控制的。
jQuery already will parse the request to an object, calling JSON.stringify
on the response was causing the object to be parsed into a JSON formatted string, removing that line resolved the issue. jQuery已经将请求解析为一个对象,在响应上调用JSON.stringify
导致该对象被解析为JSON格式的字符串,删除该行即可解决问题。 Also specifying dataType didn't seem to be necessary. 似乎也不必指定dataType。
It's worth noting that you're passing lat, and lng arguments to the function but not using them - I'd suggest either adding a lines like: 值得注意的是,您正在向函数传递lat和lng参数,但不使用它们-我建议您添加以下行:
lat = lat || sessionStorage.latitude;
lng = lng || sessionStorage.longitude;
Below should be a solution complete with my suggestions: 以下应该是一个完整的解决方案,并附上我的建议:
function getReverseGeocodingData(lat, lng) {
lat = lat || sessionStorage.latitude;
lng = lng || sessionStorage.longitude;
jsondata = {};
//use ajax and json
$.ajax({
type: "POST",
url: "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + sessionStorage.latitude + "," + sessionStorage.longitude + "&sensor=true",
data: jsondata,
success: function (response) {
var address = response.results[0].formatted_address;
var error = response.Error;
if (address != null) {
alert("Found current location: " + address);
sessionStorage.currentAddress = address;
}
},
error: function (msg) {
errorConnection();
}
});
}
just changed the success function with this, you will get all the formatted address 刚刚更改成功函数,您将获得所有格式化的地址
success: function (response) {
var address = response.results;
var error = response.error; //write exact word of the "error" which you get in response
if(address.length > 0) {
for(i=0; i<address.length; i++) {
console.log("Found current location: " + address[i].formatted_address);
}
}
},
There are a few things to fix: 有几件事需要解决:
apparently you don't have to post data as you're making a GET
request. 显然,您在发出GET
请求时不必发布数据。
I rewrote your code to take the parameters from lat
and lng
in order to test it easily. 我重写了您的代码以从lat
和lng
中获取参数,以便对其进行轻松测试。
response
is an object that contains an array of results
objects (lowercase r
plural). response
是一个包含results
对象数组的对象(小写字母r
复数)。 Each object contains a formatted_address
property. 每个对象都包含一个formatted_address
属性。
Most likely you'll want to fetch the first one: 您很可能希望获取第一个:
response.results[0].formatted_address
function getReverseGeocodingData(lat, lng) {
//use ajax and json
$.ajax(
{
type: "GET",
url: "http://maps.googleapis.com/maps/api/geocode/json?latlng="+lat+","+lng+"&sensor=true",
dataType: "json",
success: function (response) {
alert("Found current location: " + response.results[0].formatted_address);
},
error: function (msg) {
alert('error');
}
});
}
To try it: 尝试一下:
getReverseGeocodingData(44.4647452,7.3553838);
Outputs: 输出:
Found current location: Via Pasubio, 34, 12025 Dronero CN, Italia
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.