Jquery Json显示解析数据

时间:2016-03-21 07:42:09

标签: jquery json parsing

我设法通过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.   

我不明白如何把这些物品拿出来。

谢谢!

1 个答案:

答案 0 :(得分:1)

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

尝试:

var res = JSON.parse(result);
alert(res.response.total_count);
相关问题