2016-11-08 64 views
2

的API是:PHP - 不能得到價值與json_decode

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

我試圖擺脫平衡(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":""} 

我在做什麼錯?

+0

的數據位於'$ OBJ [ '數據'] [ '地址']'也許下一次做調試的一點點先用'的var_dump()' – Xorifelse

+0

@Xorifelse它應該是 '$ v =(get_object_vars($ obj-> data-> address));' '$ balance = $ v ['balance'];' 我仍然無法讓它工作。對不起,這裏有個新手。 –

+0

構造的URL:https://etherchain.org/api/account/LU4P8fVNj8xv2dwRc7fdXmpaW2xuCSJvUK,以空集返回數據:{「status」:1,「data」:[]},餘額不在其中和PHP,並會導致PHP吠叫警告,平衡不是對象的一部分(檢查警告級別和PHP日誌),除非該網址的行爲不同,如果登錄在您的機器上 – danchik

回答

1

它,因爲你錯過了地址位於第一data數組中的JSON字符串中,並在第二JSON字符串,沒有數組的數組data內部存在。

$obj = json_decode($json,true); 
$address = $obj['data'][0]['address']; 
$json = file_get_contents('https://etherchain.org/api/account/'.$address); 
$obj = json_decode($json); 
echo $obj->data[0]->balance; 
+0

我想: '$ OBJ = json_decode($ json的,真正的);' \t \t'$地址= $ obj ['data'] [0] ['address'];' '$ json = file_get_html('https://etherchain.org/api/account/'.$address); $ obj = json_decode($ json); $ v =(get_object_vars($ obj-> data [0] - > balance));' '$ balance = $ v ['balance']; $ _SESSION ['balance'] = $ balance;' 而且還沒有。 –

+0

我真的不明白你想用'get_object_vars()'做什麼,爲什麼不直接將它轉換爲數組?無論如何,更新了代碼。 – Xorifelse

+0

謝謝!它使用$ obj-> data [0] –