2016-02-29 109 views
0

其實這是一個字符串,字符串數組或對象

我怎樣才能將它轉換爲對象或數組..

{"kind": "delivery_quote", "fee": 750, "created": "2016-02-28T19:13:38Z", "expires": "2016-02-28T19:18:38Z", "currency": "usd", "duration": 60, "dropoff_eta": "2016-02-28T20:18:38Z", "id": "dqt_KhC5sbjq00Jn6F"} 

我想這

$array=explode(' ',$result); 
$json = json_encode($result); 
print($json); 

它給我像這樣的結果

"{\"kind\": \"delivery_quote\", \"fee\": 750, \"created\": \"2016-02-28T19:13:38Z\", \"expires\": \"2016-02-28T19:18:38Z\", \"currency\": \"usd\", \"duration\": 60, \"dropoff_eta\": \"2016-02-28T20:18:38Z\", \"id\": \"dqt_KhC5sbjq00Jn6F\"}" 

B UT我怎麼能做到這一點正確,這樣我可以把結果就像

echo $json->fee; 

這裏是什麼我到目前爲止的Eval

幫助請

+2

與嘗試'json_decode' – roullie

+1

你試過'json_decode '? –

+0

http://php.net/manual/en/function.json-decode.php –

回答

1
<?php 
$result = '{"kind": "delivery_quote", "fee": 750, "created": "2016-02-28T19:13:38Z", "expires": "2016-02-28T19:18:38Z", "currency": "usd", "duration": 60, "dropoff_eta": "2016-02-28T20:18:38Z", "id": "dqt_KhC5sbjq00Jn6F"}'; 

var_dump(json_decode($result)); 
var_dump(json_decode($result, true)); 
+0

它的效果很好,在5分鐘後會接受;) –

+0

Yah謝謝@SulthanA –

2

您正在嘗試編碼JSON。 json_encode返回值的JSON表示。如果你要轉換的JSON數組,那麼你應該使用json_decode爲:

$result = '{"kind": "delivery_quote", "fee": 750, "created": "2016-02-28T19:13:38Z", "expires": "2016-02-28T19:18:38Z", "currency": "usd", "duration": 60, "dropoff_eta": "2016-02-28T20:18:38Z", "id": "dqt_KhC5sbjq00Jn6F"}'; 
$json = json_decode($result); 
echo $json->fee; 

哪位能給作爲輸出費的價值:

750 
相關問題