简体   繁体   中英

Extracting data from JSON with ASP.Net web service

I am trying unsuccessfully to extract the formatted_address property.

The following web service logs the JSON below to the console. I cannot get the formatted address using returnedData.d.results[0].formatted_address .

 $.ajax({
        type: "POST",
        url: "ReportIncident.aspx/ReverseGeocode",
        data: "{latitude:" + latitude + ",longitude:" + longitude + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (returnedData)
        {
          console.log(returnedData);
        }
    });

The format of the json is the exact same as the format over here at Google .

Edit Darin pointed out that I was contradicting myself: the web service wraps up everything in the link above in ad object, I failed to mention that.

在此处输入图片说明

Further edit Here is the web service:

[WebMethod]
        public static string ReverseGeocode(decimal latitude, decimal longitude)
        {
            // Create the web request  

            string url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + latitude + "," + longitude +"&sensor=true";
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

            // Get response  
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                // Get the response stream  
                StreamReader reader = new StreamReader(response.GetResponseStream());

                return reader.ReadToEnd();
            }
        }

and here is the javascript:

/Gets the current location of the user
function getLocation()
{
    if (navigator.geolocation)
    {
        navigator.geolocation.getCurrentPosition(showPosition);
    }

}

function showPosition(position)
{
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;

    $.ajax({
        type: "POST",
        url: "ReportIncident.aspx/ReverseGeocode",
        data: "{latitude:" + latitude + ",longitude:" + longitude + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (returnedData)
        {
            alert(returnedData.d[0].results[0].formatted_address);
            console.log(returnedData);
        }
    });

Have you tried using returnedData.results[0].formatted_address without the .d. node. That does not exist!

remove the ".d"

returnedData.results[0].formatted_address

but doing this you are always getting the first node only

The answer to this issue turns out to be that the web service is returning a string, instead of json. The built-in javascript serializer does not get used. The eval keyword or something more secure needs to be used. As it happens I ended up using the Google Maps Javascript API and that was much easier anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM