【问题标题】:How to access data from JSON object?如何从 JSON 对象访问数据?
【发布时间】:2015-07-30 22:45:49
【问题描述】:

http://i.stack.imgur.com/9Pha0.png 这是我的 JSON 的一部分。我需要访问photo_file_url

我试着写成

$.ajax( {
        url: "http://www.panoramio.com/map/get_panoramas.php?set=public&from=0&to=1&minx="+longitude+"&miny="+latitude+"&maxx="+lonmax+"&maxy="+latmax+"&size=original&mapfilter=true",
        type: "get",
        success: function(response) {
        document.getElementById("back").innerHTML = response.photos.photo_file_url;
        }
});

但它不起作用

【问题讨论】:

标签: javascript jquery json object


【解决方案1】:

由于您正在执行异步 GET 请求,因此您也可以使用

$.getJSON("http://www.panoramio.com/map/get_panoramas.php?set=public&from=0&to=1&minx="+longitude+"&miny="+latitude+"&maxx="+lonmax+"&maxy="+latmax+"&size=original&mapfilter=true", function(response) {
  // response will be a json object
  document.getElementById("back").innerHTML = response.photos.photo_file_url;
});

我仅推荐此解决方案,因为您正在执行与 $.getJSON() 同义的异步 GET 请求

【讨论】:

    【解决方案2】:
    $.ajax( {
            url: "http://www.panoramio.com/map/get_panoramas.php?set=public&from=0&to=1&minx="+longitude+"&miny="+latitude+"&maxx="+lonmax+"&maxy="+latmax+"&size=original&mapfilter=true",
            type: "get",
            success: function(response) {
            response = $.parseJson(response); //parse json object
            document.getElementById("back").innerHTML = response.photos.photo_file_url;
            }
    });
    

    【讨论】:

      【解决方案3】:
      success: function(response) {
          document.getElementById("back").innerHTML = JSON.parse(response).photos[0].photo_file_url;
      }
      

      或者

      $.ajax({
          dataType: "json"
          succes: ...
      

      或$.getJSON

      【讨论】:

        【解决方案4】:

        Ajax-Parse JS/JQuery

        How to access JSON object in JavaScript

        我不知道这些会有多大帮助,但希望这是一个起点。

        【讨论】:

          【解决方案5】:

          您的响应是一个字符串,您可以使用JSON.parse 将其转换为一个对象。

          编辑:为了兼容性$.parseJSON。 See this answer.

          【讨论】:

            猜你喜欢
            • 2018-04-18
            • 2017-11-03
            • 1970-01-01
            • 1970-01-01
            • 2022-11-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多