dongqiangse6623 2016-05-03 22:00
浏览 79
已采纳

如何在php中定位嵌套的JSON对象,并回显它们?

Okay, so I think you get what I wanna do by just watching the code.

//Get JSON text file (Steam API)
$json = file_get_contents('http://store.steampowered.com/api/appdetails?appids=57690');

//Decode JSON
$game_json = json_decode($json, true);

//Target game name and echo it
echo $game_json['name'];

The JSON itself comes in this order (unstructured, very sorry):

{"57690":{"success":true,"data":{"type":"game","name":"Tropico 4: Steam Special Edition"

So my target is ""name":"Tropico 4: Steam Special Edition"", which is what I want to echo on my page. I'm not sure if it helps, but "name": appears once, is something like [0] needed in my code to target the first? Is the nesting what's stopping me here, or is the $game_json['name']; an incorrect way of targeting?

ANY tips and/or help will be much appreciated. Thanks.

  • 写回答

2条回答 默认 最新

  • dragon188199 2016-05-03 22:05
    关注

    In the future, use print_r($game_json) to check the array structure.

    <?php
    $json = file_get_contents('http://store.steampowered.com/api/appdetails?appids=57690');
    $game_json = json_decode($json, true);
    echo $game_json['57690']['data']['name'];
    //Tropico 4: Steam Special Edition
    echo $game_json['57690']['data']['required_age'];
    //0
    //etc...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!