douejuan9162 2016-01-28 19:57
浏览 53

动态JSON输出

For a tool I am currently making, it outputs JSON and I decode it using PHP and then echo it via the same script. In said JSON, some arrays are static and some change, such as a job id.

For example, for a request, you may get an array such as

{ "rank": "Supreme Damage Dealer", "player_id": Name, "name": "Name",

in which case, rank, player_id, and name are all static and the only thing that changes is the output.

In some arrays, such as

{ "crimes": { "4769740": { "crime_id": 3, "crime_name": "Bomb threat", "participants": "1616976,1848006,1829524", "time_started": 1453948278, "time_completed": 1454207478, }, "4769739": { "crime_id": 4, "crime_name": "Planned robbery", "participants": "612285,1603035,579999,1858750,1875355", "time_started": 1453948245, "time_completed": 1454293845, },

The numbers such as 4769740 and 4769739 changes, and therefore I cannot output it as I would name/rank, because unlike name/rank the title changes.

I need to output it onto a page the same why I would the name and rank. Currently, the name and rank for example are outputted like so:

$jsonurl = "http://api.torn.com/user/$id?selections=basic&key=$key";
$json = file_get_contents($jsonurl);
$decodedString = json_decode($json, true);
//var_dump($decodedString);
echo "Level: ".$decodedString["level"]."</br>";
echo "Name: ".$decodedString["name"]."</br>";

however I cannot do the same for the crimes. How would I output the crime data?

Using code, $jsonurl = "http://api.torn.com/faction/7709?selections=crimes&key=key"; $json = file_get_contents($jsonurl); $decodedString = json_decode($json); foreach($decodedString as $key => $value){ //At this step $key is 4769740 //$value is an array of the values inside echo "Level: ".$value["crime_name"]."</br>"; }

I get the error message Fatal error: Cannot use object of type stdClass as array in /var/www/html/torn/Scripts/Faction/crimes.php on line 19

  • 写回答

1条回答 默认 最新

  • dongxinpa3101 2016-01-28 20:25
    关注

    $decodedString would return as an object for your json. In this case it would only have one element, crimes which is another object that holds crime objects. Each one of those crime objects contains the data you're looking for.

    foreach($decodedString as $key => $value){
        //At this step $key is the string "crimes" and the value is the object of objects inside
        foreach($value as $number => $crime){
            //Now $crime is an object of values for each crime
            echo "Level: ".$crime->crime_name."</br>";
        }
    }
    

    Would output:
    Bomb threat
    Planned robbery

    You could potentially skip the first foreach if you know crimes is the only object there. print_r() is your friend for debugging.

    foreach($decodedString->crimes as $number => $crime){
        //$crime is the object with the data you're looking for. 
        echo "Level: ".$crime->crime_name."</br>";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题