简体   繁体   English

使用ASP.Net Web服务从JSON提取数据

[英]Extracting data from JSON with ASP.Net web service

I am trying unsuccessfully to extract the formatted_address property. 我尝试提取formatted_address属性失败。

The following web service logs the JSON below to the console. 以下Web服务将以下JSON记录到控制台。 I cannot get the formatted address using returnedData.d.results[0].formatted_address . 我无法使用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 . json的格式与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. Edit Darin指出,我在矛盾自己:Web服务在广告对象的上面链接中包装了所有内容,但我没有提及。

在此处输入图片说明

Further edit Here is the web service: 进一步编辑这是Web服务:

[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: 这是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. 您是否尝试过使用不带.d. returnedData.results[0].formatted_address .d. node. 节点。 That does not exist! 那不存在!

remove the ".d" 删除“ .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. 这个问题的答案原来是Web服务正在返回一个字符串,而不是json。 The built-in javascript serializer does not get used. 内置的javascript序列化程序无法使用。 The eval keyword or something more secure needs to be used. 需要使用eval关键字或更安全的名称。 As it happens I ended up using the Google Maps Javascript API and that was much easier anyway. 碰巧的是,我最终使用了Google Maps Javascript API,无论如何这要容易得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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