【问题标题】:Populate HTML with JSON using JavaScript使用 JavaScript 使用 JSON 填充 HTML
【发布时间】:2017-03-15 16:17:53
【问题描述】:

有点麻烦。目标是使用 JavaScript 中请求的 JSON 数据填充 HTML 页面。我已经成功在文档中显示了http请求代码和状态。但是,我无法成功显示 JSON 中的数据。我不确定 JS 的第 7 行是否正确,因为我是从一个示例中得出的。同样在 JS 中,第 13 行是我无法工作的行。 (我只想使用 JavaScript 和 HTML——没有 AJAX、JQUERY 等) 任何建议都会有所帮助,到目前为止我还没有在这个网站上找到任何与我的问题相似的东西。

下面是 HTML

<!DOCTYPE html>
<html>
<head>
    <script src="NEO.js"></script>
<title>NEO</title>
</head>
<body onload="getData()">
<p id="xhrStatus">text</p>
<p id="xhrText">text1</p>
<p id="neoName">name</p>
</body>
</html>

下面是 JS

function getData()
{
var xhr = new XMLHttpRequest();
xhr.open("GET", "ADDRESS LEFT BLANK INTENTIONALLY FOR STACKOVERFLOW", false);
xhr.send(null)
var responseData = JSON.parse(xhr.response);
//the line above is base off of an example. Not sure if it's correct

document.getElementById("xhrStatus").innerHTML = xhr.status;
document.getElementById("xhrText").innerHTML = xhr.statusText;

//Having trouble with line below. Not grabbing data correctly.
var neoName = responseData.near_earth_objects.2017-03-15[0];
document.getElementById("neoName").innerHTML = neoName;
}

下面是 JSON(来自地址栏中的调用)

{
  "links": {
    "next": "ADDRESS LEFT OUT INTENTIONALLY",
    "prev": "ADDRESS LEFT OUT INTENTIONALLY",
    "self": "ADDRESS LEFT OUT INTENTIONALLY"
  },
  "element_count": 5,
  "near_earth_objects": {
    "2017-03-15": [
      {
        "links": {
          "self": "ADDRESS LEFT OUT INTENTIONALLY"
        },
        "neo_reference_id": "2011398",
        "name": "11398 (1998 YP11)",
        "nasa_jpl_url": "ADDRESS LEFT OUT INTENTIONALLY",
        "absolute_magnitude_h": 16.3,
        "estimated_diameter": {
          "kilometers": {
            "estimated_diameter_min": 1.4606796427,
            "estimated_diameter_max": 3.2661789745
          },
          "meters": {
            "estimated_diameter_min": 1460.6796427136,
            "estimated_diameter_max": 3266.1789744576
          },
          "miles": {
            "estimated_diameter_min": 0.9076239703,
            "estimated_diameter_max": 2.0295088955
          },
          "feet": {
            "estimated_diameter_min": 4792.2561990004,
            "estimated_diameter_max": 10715.8106265596
          }
        },
        "is_potentially_hazardous_asteroid": false,
        "close_approach_data": [
          {
            "close_approach_date": "2017-03-15",
            "epoch_date_close_approach": 1489561200000,
            "relative_velocity": {
              "kilometers_per_second": "7.6545148105",
              "kilometers_per_hour": "27556.2533179201",
              "miles_per_hour": "17122.384179682"
            },
            "miss_distance": {
              "astronomical": "0.2788205228",
              "lunar": "108.4611816406",
              "kilometers": "41710956",
              "miles": "25917986"
            },
            "orbiting_body": "Earth"
          }
        ],
        "orbital_data": {
          "orbit_id": "232",
          "orbit_determination_date": "2017-03-14 06:21:59",
          "orbit_uncertainty": "0",
          "minimum_orbit_intersection": ".202697",
          "jupiter_tisserand_invariant": "4.048",
          "epoch_osculation": "2457800.5",
          "eccentricity": ".3889800628971847",
          "semi_major_axis": "1.720520012229252",
          "inclination": "15.02606696200692",
          "ascending_node_longitude": "144.8173241683082",
          "orbital_period": "824.3051621591791",
          "perihelion_distance": "1.051272029656453",
          "perihelion_argument": "74.64055539736296",
          "aphelion_distance": "2.389767994802052",
          "perihelion_time": "2457881.692298597634",
          "mean_anomaly": "324.5407685928043",
          "mean_motion": ".4367314636936381",
          "equinox": "J2000"
        }
      }

【问题讨论】:

  • 尝试:responseData.near_earth_objects.['2017-03-15'][0];。我不相信你可以在那个键上使用点符号。

标签: javascript html json dom


【解决方案1】:

你不能使用 - 没有在一个字符串中 并且您将内部 html 设置为 javascript 对象而不是字符串

试试这个

var neoName = responseData.near_earth_objects["2017-03-15"][0].neo_reference_id;

如果你想显示整个对象,你可以使用stringify()

var neoName = JSON.stringify(responseData.near_earth_objects["2017-03-15"][0]);

【讨论】:

    猜你喜欢
    • 2015-10-24
    • 1970-01-01
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    • 2014-12-26
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多