简体   繁体   English

JSON解码到PHP无法正常工作

[英]JSON decode to PHP not working

I try to put a JSON Object into a PHP Variable, it is not working. 我尝试将JSON对象放入PHP变量,但无法正常工作。 When I var_dump it, it says NULL . 当我var_dump时,它说NULL

PHP: PHP:

<?php
$json   = utf8_encode(file_get_contents('http://socialclub.rockstargames.com/ajax/stat/1/profile_body/sta1/drantifat'));
$data   = json_decode($json, false);

$money = $data->tplc->stats;   // -> What do i have to do to reach the id:GTA Online Cash and use the val: $33.9K ?
?>

JSON file example: (it changes over time) JSON文件示例:(随时间变化)

{"cid":0,"ttl":"Grand Theft Auto V",
"ishs":false,"issa":false,"istc":false,"htl":true,"tlc":"text",
"tlt":"View Stats","tlh":"/member/drantifat/games/gtav/career/overview",
"iso":false,"cmng":false,"order":0,"uid":0,"ttc":0,
"tplc":"{\"game\":\"GTAV\",\"stats\":[{\"id\":\"Game Progress\",\"val\":\"58.77%\"},
{\"id\":\"Missions Passed\",\"val\":\"55\"},{\"id\":\"Playing Time\",\"val\":\"29:00:33\"},
{\"id\":\"GTA Online RP\",\"val\":\"2.4M\"},{\"id\":\"GTA Online Rank\",\"val\":\"128\"},
{\"id\":\"GTA Online Cash\",\"val\":\"$33.9K\"},
{\"id\":\"GTA Online Playing Time\",\"val\":\"529:44:12\"}],
\"url\":\"/member/drantifat/games/gtav/career/overview\"}","isa":false,"cnt":""}

Could anyone also help me with what I've mentioned in the PHP file? 有人能帮我解决我在PHP文件中提到的问题吗?

tplc isn't a JSON object, it's a string that is itself JSON. tplc不是JSON对象,而是一个本身就是JSON的字符串。

<?php
$json = utf8_encode(file_get_contents('http://socialclub.rockstargames.com/ajax/stat/1/profile_body/sta1/drantifat'));
$data = json_decode($json, false);
$tplc = json_decode($data->tplc);
$money = $tplc->stats;
?>

To get the money value specifically: 要具体获得货币价值:

<?php
$json = utf8_encode(file_get_contents('http://socialclub.rockstargames.com/ajax/stat/1/profile_body/sta1/drantifat'));
$data = json_decode($json, false);
$tplc = json_decode($data->tplc);
$stats = $tplc->stats;
foreach ($tplc->stats as $stat) {
    if ($stat->id == 'GTA Online Cash') {
        $money = $stat->val;
        break;
    }
}
echo $money;
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM