在PHP

2013-03-22 64 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