2014-01-10 23 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" 

你不想直接提取变量到你的程序的本地词汇范围,因为这会带来安全担忧。只需使用上面代码段中指出的数据即可。