【问题标题】:Calling Correct Values of JSON from nested objects从嵌套对象调用 JSON 的正确值
【发布时间】:2018-10-12 18:07:46
【问题描述】:

我在 JSON 中调用,返回类似于

这是 JSON 文件的实际部分。不仅如此,我只是想展示如何获得嵌套值。

{
  player: {
    id: 277013255,
    game: "bf4",
    plat: "pc",
    name: "f1ss1on",
    tag: "MCG",
    dateCheck: 1524851054474,
    dateUpdate: 1524849279114,
    dateCreate: 1385342286868,
    dateStreak: 1524849279114,
    lastDay: "20160222",
    country: "",
    countryName: null,
    rank: {
      nr: 33,
      imgLarge: "bf4/ranks/r33.png",
      img: "r33",
      name: "Master Sergeant III",
      needed: 1280000,
      next: {
        nr: 34,
        imgLarge: "bf4/ranks/r34.png",
        img: "r34",
        name: "Master Sergeant IV",
        needed: 1345000,
        curr: 1317090,
        relNeeded: 65000,
        relCurr: 37090,
        relProg: 57.06153846153846
      }
    },

我的代码是:

$(document).ready(function() {
  "use strict";

  var html = '';
  $("button").click(function() {
    $.getJSON("https://api.bf4stats.com/api/playerInfo?plat=pc&name=f1ss1on&output=json", function(result) {
      $.each(result, function(i, entry) {
        html += '<ul>';
        html += '<li class="name col-5">' + entry.name + '</li>';
        html += '<li class="date col-3">' + entry.tag + '</li>';
        html += '<li class="assigned col-4">' + entry.rank + '</li>';
        html += '</ul>';
      });
    }
    $('div').html(html);
  });
});

预期回报应该是

  • f1ss1on
  • MCG
  • 33

但相反,我得到了这个回报:

  • f1ss1on
  • MCG
  • [对象对象]

  • 未定义

  • 未定义
  • 33

如何正确地将嵌套的 JSON 对象调用到正确的元素上?

我已经尝试过:

entry.rank.nr

但这会返回“Uncaught TypeError: Cannot read property 'nr' of undefined”

【问题讨论】:

  • 你的JSON声明太奇怪了,我看不懂,为什么你要打电话给28,你写entry.rank,你应该写entry.rand.level
  • 这不是有效的 JSON。
  • 无效的 JSON 和无效的 JavaScript。请粘贴网络响应的实际内容,或在解析后的响应上执行JSON.stringify(result)
  • 我没有给出完整的 json 只是想展示它是如何嵌套的示例。

标签: javascript html json ajax


【解决方案1】:

解决方案

您调用的端点不会返回多个玩家,它会返回一个 player 对象,因此我不确定是否需要任何类型的循环或 each

以下似乎工作正常:

$.getJSON("https://api.bf4stats.com/api/playerInfo?plat=pc&name=f1ss1on&output=json", function(result) {
  var player = result.player;
  console.log("Name:", player.name);
  console.log("Tag:", player.tag);
  console.log("Rank:", player.rank.nr);
});
&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;

结果说明

要准确显示查询中出了什么问题,请打开浏览器控制台并查看此内容。这是您的 result 表格形式。

$.getJSON("https://api.bf4stats.com/api/playerInfo?plat=pc&name=f1ss1on&output=json", function(result) {
  console.table(result);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

If the table doesn't appear in your console, re-run this snippet with the console already open.

在这里,您可以在表格中以行的形式查看循环的项目。

第一个项目是player,这就是为什么nametag 正常返回,但rank 作为对象返回。

第二项是stats。此行中的结果解释了为什么您的第二次迭代返回 nametag 作为 undefined,但 rank 作为 33

我想在那之后你会收到一大堆undefineds(每个项目三个)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多