【问题标题】:How to get JSON objects from web service response如何从 Web 服务响应中获取 JSON 对象
【发布时间】:2014-09-24 08:51:18
【问题描述】:

我对使用 Web 服务、javascript、JSON 技术非常陌生,我需要使用 URL 来获取一些数据以在我的 HTML 文件中使用。

我试图获取价值的网址类似于this

这个url在浏览器中的结果如下:

{
  "transactionid": "asdf", 
  "status": 0,
  "poilist": [
    {
      "id": 123,
      "name": "some company",
      "address": "address",
      "latitude": 333333,
      "longitude": 333333,
      "distance": 4869
    },
    {
      .... // lots of similar nodes to above
    }
}

我需要获取 poilist 列表的一些属性,例如经度、纬度等,并在我的 HTML 文件中使用它们,该文件仅包含 Javascript 和 HTML 代码。

我在互联网上进行了一些研究,但找不到适合我情况的示例。我不知道从哪里开始。任何帮助将不胜感激。

谢谢。

【问题讨论】:

    标签: html json web-services url object


    【解决方案1】:

    你可以这样:

    var url = 'http://www.locationbox.com.tr/locationbox/services?Key=key&Cmd=PoiSearch&Typ=JSON&Latitude=30&Longitude=30&Radius=10000&Brand=SomeBrand&Keyword=';
    
    $('#test').on('click', function () {
        $.ajax({
            url: url,
    
            // the name of the callback parameter, as specified by the YQL service
            jsonp: "callback",
    
            // tell jQuery we're expecting JSONP
            dataType: "jsonp",
    
    
    
            // work with the response
            success: function (response) {
                //response is an object. use it here
                console.log(response); // server response
            }
        });
    });
    

    http://jsfiddle.net/hlapidez/sm64g/

    【讨论】:

    • 嗨,汉克,非常感谢您的回复。这就是我一直在寻找的。 jsfiddle 是一个非常不错的网站。我不知道它..
    • jsfiddle 很适合快速尝试。
    【解决方案2】:
       Hope this helps for u.
    
        Here poilist is an JsonArray.
                So u have to iterate poilist and get poilist properties
    
                javascript example
    
                    var response = "{ "transactionid": "asdf", "status": 0, "poilist": [ { "id": 123, "name": "some company", "address": "address", "latitude": 333333, "longitude": 333333, "distance": 4869 },... ";
    
                    var poiList = response.poilist;
                    for(var i=0;i<poiList.length;i++){
                      var name = poiList[i].name;
                      var id= poiList[i].id;
                      var lat = poiList[i].latitutde;
                      var long = poiList[i].longitude;
                      console.log(lat);
                      console.log(long);
                    }
    
            This code will print all properties of poilist in browser's console.
    

    【讨论】:

    • 您好,感谢您的回复。我想有一个像你在上面写的名为 response 的代码的对象,但不要将所有 JSONArray 复制并粘贴到我的 HTML 文件中。有没有办法获得完整的 JSONArray 给出的 url?
    • 你在使用 Ajax 从服务器获取数据吗?
    • 其实问题是,我不知道用什么,也不知道怎么做。我也不知道使用 Ajax。
    • 1.加载 Jquery.js,我们的一些朋友在下面发布了一些关于如何进行 ajax 调用的代码示例。请参考我在答案中提到的响应变量中的存储相同
    【解决方案3】:

    我会从 jQuery 开始,特别是 jQuery.getJSON()。阅读它here

    如果你没有使用过 jQuery 并且不知道如何使用它。我会看这里first

    加载数据并在控制台中显示它们的非常基本的示例如下所示:

    $(document).ready(function() {
    
        var url = ""; //enter an url here
    
        $.getJSON(url, function( data ) {
            console.log(data);
        });
    
    });
    

    【讨论】:

    • 嗨大卫,谢谢你的回复,它帮助了很多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 2019-02-03
    相关资源
    最近更新 更多