在PHP

2013-03-22 65 views
0
從JSON數組提取特定鍵值

我有一個支付網關(JSON)以下回應:在PHP

{ 
    "response": { 
    "token": "ch_lfUYEBK14zotCTykezJkfg", 
    "success": true, 
    "amount": 400, 
    "currency": null, 
    "description": "test charge", 
    "email": "[email protected]", 
    "ip_address": "203.192.1.172", 
    "created_at": "2012-06-20T03:10:49Z", 
    "status_message": "Success!", 
    "error_message": null, 
    "card": { 
     "token": "card_nytGw7koRg23EEp9NTmz9w", 
     "display_number": "XXXX-XXXX-XXXX-0000", 
     "scheme": "master", 
     "address_line1": "42 Sevenoaks St", 
     "address_line2": null, 
     "address_city": "Lathlain", 
     "address_postcode": "6454", 
     "address_state": "WA", 
     "address_country": "Australia" 
    }, 
    "transfer": null 
    } 
} 

我想if語句,像這樣得到success => true部分在:

<?php 
    if($response['success'] == true) { 
    // continue transaction 
    } 
?> 

沒有任何運氣,所以我要求一些幫助,我如何實現這一目標。能夠提取其他關鍵值也是有用的。

感謝

+1

有是一個類似於'json_decode'的函數。 – 2013-03-22 07:34:34

+1

在PHP代碼的情況下,你爲什麼不解碼你的json&try。 – Rikesh 2013-03-22 07:34:38

回答

2

你是不是能夠json_decode($response)的原因是因爲$response是一個對象,而不是一個JSON字符串。

引腳PHP庫會自動解碼爲你的所有要求,按照https://github.com/noetix/pin-php/blob/master/src/Pin/Handler.php#L87

要看到,如果反應是成功的,使用響應對象作爲一個對象:

if ($response->response->success) { 
    echo "Success!"; 
} else { 
    echo "Failed :("; 
} 
3

更新

$response已經是一個對象,因此這種表達應該工作:

if ($response->response->success == true) { 
    // etc 
} 

你只是尋找一個字符串表示解碼碼?如果是這樣的:

$response = <<<'EOM' 
{ 
    "response": { 
    "token": "ch_lfUYEBK14zotCTykezJkfg", 
    "success": true, 
    "amount": 400, 
    "currency": null, 
    "description": "test charge", 
    "email": "[email protected]", 
    "ip_address": "203.192.1.172", 
    "created_at": "2012-06-20T03:10:49Z", 
    "status_message": "Success!", 
    "error_message": null, 
    "card": { 
     "token": "card_nytGw7koRg23EEp9NTmz9w", 
     "display_number": "XXXX-XXXX-XXXX-0000", 
     "scheme": "master", 
     "address_line1": "42 Sevenoaks St", 
     "address_line2": null, 
     "address_city": "Lathlain", 
     "address_postcode": "6454", 
     "address_state": "WA", 
     "address_country": "Australia" 
    }, 
    "transfer": null 
    } 
} 
EOM; 

$responseData = json_decode($responseText, true); 

if ($responseData['response']['success'] == true) { 
// continue transaction 
} 
+0

那裏還有一個''response''鍵。 – deceze 2013-03-22 07:36:30

+0

這將返回null,我之前已經嘗試過。代碼:http://codepad.org/BmAT3bMY $ responseData = NULL但是$ response確實有JSON數組。 – Dean 2013-03-22 07:40:47

+0

我也試過http://stackoverflow.com/questions/2410342/php-json-decode-returns-null-with-valid-json沒有成功。有小費嗎? – Dean 2013-03-22 07:50:16

0

爲什麼不直接使用像一個JSON對象:

$jsonobject = json_encode($responseText); 
$response = $jsonobject->response->success; 
if($response === true) { 
    // code here 
} 
+0

相同的交易http://stackoverflow.com/questions/15565067/extracting-specific-key-value-from-json-array-in-php#comment22061209_15565109它返回null。 – Dean 2013-03-22 07:54:18