PHP - 无法通过json_decode获取值

时间:2016-11-08 02:33:06

标签: php json

API是:

{"status":1,"data":[{"address":"0x5c48aebbbbdcf44f5f181edbb5b20a68210eacfe","balance":6000000000000000000,"nonce":null,"code":"0x","name":null,"storage":null,"firstSeen":null}]}

我试图从Balance(6000000000000000000)获取值。我还需要在此期间将此值除以1000000000000000000 ......

我试过了:

$obj = json_decode($json,true);
$address = $obj['address'];

$json = file_get_html('https://etherchain.org/api/account/'.$address);
$obj = json_decode($json);
$v = (get_object_vars($obj->data));
$balance = $v['balance'];
$_SESSION['balance'] = $balance;

它不起作用。

相同的代码适用于以下API:

{"status":"success","data":{"address":"LU4P8fVNj8xv2dwRc7fdXmpaW2xuCSJvUK","is_unknown":false,"balance":0,"balance_multisig":0,"totalreceived":0.0340576,"nb_txs":11,"first_tx":{"time_utc":"2015-08-30T09:51:05Z","tx":"10f4b55d0bead8d3d84fe27433db20b63368d65bc043c651d59dbe1342d14098","block_nb":"842330","value":0.00704229,"confirmations":251241},"last_tx":{"time_utc":"2016-02-02T16:09:23Z","tx":"c666d3acf6f57fd86a2ccc9537ee022167da408f26193e4abbd7b8148fc518b3","block_nb":"932771","value":-0.01250452,"confirmations":160800},"is_valid":true},"code":200,"message":""}

我做错了什么?

1 个答案:

答案 0 :(得分:1)

因为您错过了地址位于JSON字符串中的第一个 data数组和第二个JSON字符串中,{{1}内部不存在任何数组} array。

data