【问题标题】:Jquery Json Display Parsed DataJquery Json 显示解析后的数据
【发布时间】:2016-03-21 07:42:09
【问题描述】:

我已经设法通过 ajax 取回数据,然后解析它,但我不明白如何获取这些项目。

API 链接:

http://api.steampowered.com/IPlayerService/GetRecentlyPlayedGames/v0001/?key=mytid&steamid=76561198033943113&format=json

JSON 响应

{
  "response": {
    "total_count": 1,
    "games": [{
      "appid": 252950,
      "name": "Rocket League",
      "playtime_2weeks": 1631,
      "playtime_forever": 28185,
      "img_icon_url": "217214f6bd922a8da8bdd684aa94b1ef8e7724d1",
      "img_logo_url": "58d7334290672887fdd47e25251f291b812c895e"
    }]

  }
}

我正在向您显示链接,以便您了解层次结构。

所以是的,我成功地将数据返回并解析它:

JSON.parse(result);

然后我做:

alert(result); //- works and shows me the data.
alert(result.response.total_count); // - doesn't work.   

我不明白如何取出物品。

谢谢!

【问题讨论】:

  • 你能把响应对象发到result吗?
  • 尝试使用 JSON.parse(result) 的返回值;例如stackoverflow.com/a/4935684/2115381
  • Hm 类似 obj =JSON.parse(result);日志(obj.response....)

标签: jquery json parsing


【解决方案1】:

你认为alert(result); 工作是因为你看到了数据,但实际上它工作不正常,因为你不应该通过警告对象来看到数组数据。 JSON.parse() 将返回包含您的 json 数组的对象,因此您需要将其分配给变量,因为它不会覆盖您的 result 变量...

试试:

var res = JSON.parse(result);
alert(res.response.total_count);

【讨论】:

    猜你喜欢
    • 2019-11-19
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多