2014-01-10 62 views
-1

我試圖解碼json回調。在php中解碼json並創建變量

JSON的代碼被髮布到callback.php - 這裏是JSON的一個例子:

{ 
"order": { 
"id": "5RTQNACF", 
"created_at": "2012-12-09T21:23:41-08:00", 
"status": "completed", 
"total_btc": { 
    "cents": 100000000, 
    "currency_iso": "BTC" 
}, 
"total_native": { 
    "cents": 1253, 
    "currency_iso": "USD" 
}, 
"custom": "order1234", 
"receive_address": "1NhwPYPgoPwr5hynRAsto5ZgEcw1LzM3My", 
"button": { 
    "type": "buy_now", 
    "name": "Alpaca Socks", 
    "description": "The ultimate in lightweight footwear", 
    "id": "5d37a3b61914d6d0ad15b5135d80c19f" 
}, 
"transaction": { 
    "id": "514f18b7a5ea3d630a00000f", 
    "hash": "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b", 
    "confirmations": 0 
}, 
"customer": { 
    "email": "[email protected]", 
    "shipping_address": [ 
    "John Smith", 
    "123 Main St.", 
    "Springfield, OR 97477", 
    "United States" 
    ] 
} 
} 
} 

我可以附和JSON,並得到如下回應:如果我

{"order""id":null,"created_at":null,"status":"completed","total_btc":{"cents":100000000,"currency_iso":"BTC"},"total_native":{"cents":83433,"currency_iso":"USD"},"custom":"123456789","receive_address":"1A2qsxGHo9KjtWBTnAopTwUiBQf2w6yRNr","button":{"type":"buy_now","name":"Test Item","description":null,"id":null},"transaction":{"id":"52d064b59eeb59985e00002c","hash":"4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b","confirmations":0}}} 

然而嘗試解碼json使用以下內容:

$array = json_decode($jsonString, true); 
echo $array; 

我得到以下響應:「200陣列」

我希望能夠將每個json參數都變成一個php變量。

+4

如果你想看到它的字符串輸出,不要'echo'數組'var_dump()'或'print_r()'。 –

+1

可能重複[在PHP中從json回調中獲取POST數據](http://stackoverflow.com/questions/21052500/get-post-data-from-a-json-callback-in-php) – mario

+0

而那200不是由發佈的代碼生成的,但是我猜測腳本中的其他地方會迴應HTTP請求的結果並獲得200響應代碼。 –

回答

2

您可以通過做內$array訪問的變量,例如:

echo $array['custom']; // prints out "order1234" 

你不想直接提取變量到你的程序的本地詞彙範圍,因爲這會帶來安全擔憂。只需使用上面代碼段中指出的數據即可。