[英]How to get JSON response in to PHP variables
I am getting following response in JSON format and I want it to convert it into PHP variables. 我正在获取JSON格式的响应,并且希望它将其转换为PHP变量。
JSON: JSON:
{"CreateTransactionResponse":{"CreateTransactionResult":{"TransportKey":"aa900d54-7bfb-47e9-a5de-e423ec34a900"
,"ValidationKey":"fbb28b32-f439-4801-a434-99c70aa388ca","Messages":{}}}}
output should be PHP: 输出应为PHP:
$transkey = aa900d54-7bfb-47e9-a5de-e423ec34a900;
$vkey = fbb28b32-f439-4801-a434-99c70aa388ca
please advice me how to do it. 请告诉我该怎么做。
If you want to access your json try to decode it first: 如果要访问您的json,请尝试先对其进行解码:
$result = json_decode($yourJSON, true);
foreach($result['CreateTransactionResponse'] as $key => $val){
echo $transkey = 'TransportKey= ' . $val['TransportKey'] . '<br/>;
echo $vkey = 'ValidationKey= ' . $val['ValidationKey'];
}
Or if it is an array of JSON's 或者,如果它是JSON的数组
$result = json_decode($yourJSON, true);
$data = [];
foreach($result['CreateTransactionResponse'] as $key => $val){
$data[] = [
'TransportKey' => $val['TransportKey'],
'ValidationKey' => $val['ValidationKey']
];
}
print_r($data);
Just simply use json_decode(); 只需简单地使用json_decode();
$result= json_decode($jSon);
var_dump($result); // to see the output
json to array( json_decode
) and then extract
from array. json到array(
json_decode
),然后从数组中extract
。
$arr = json_decode($json, true);
extract($arr);
var_dump($CreateTransactionResponse);
Output: 输出:
array (size=1)
'CreateTransactionResult' =>
array (size=3)
'TransportKey' => string 'aa900d54-7bfb-47e9-a5de-e423ec34a900' (length=36)
'ValidationKey' => string 'fbb28b32-f439-4801-a434-99c70aa388ca' (length=36)
'Messages' =>
array (size=0)
empty
use $CreateTransactionResult['TransportKey']
to access Transport Key from JSON. 使用
$CreateTransactionResult['TransportKey']
从JSON访问传输密钥。 Similarly $CreateTransactionResult['ValidationKey']
for Validation Key. 类似地,
$CreateTransactionResult['ValidationKey']
用于验证密钥。
try this code it will work
$JSON='{"CreateTransactionResponse":{"CreateTransactionResult":{"TransportKey":"aa900d54-7bfb-47e9-a5de-e423ec34a900" ,"ValidationKey":"fbb28b32-f439-4801-a434-99c70aa388ca","Messages":{}}}}';
$arr=json_decode($JSON, TRUE);
foreach ($arr as $value) {
foreach ($arr['CreateTransactionResponse'] as $key => $var) {
echo 'TransportKey = '.$var['TransportKey'].'<br>';
echo 'ValidationKey = '.$var['ValidationKey'].'<br>';
foreach ($var['Messages'] as $key => $msg) {
echo 'Messages = '.$msg.'<br>';
}
}
}
In this case,If its a single and TransportKey
and a single ValidationKey
value (not an array/object is passed) at a time, this is the simplest . 在这种情况下,如果一次同时使用一个和
TransportKey
以及一个ValidationKey
值(不传递数组/对象),则这是最简单的 。 Else if object contains objects or inside objects that we want to use or convert to variable, should use a foreach
to loop through the object. 否则,如果对象包含我们要使用或转换为变量的对象或内部对象,则应使用
foreach
该对象。
//Debuggig
//The string you provided is converted to a json object
//In your case if it is a json object already pass directly it to $j
//below is just for debugging and understanding
//$json='{"CreateTransactionResponse":{"CreateTransactionResult":{"TransportKey":"aa900d54-7bfb-47e9-a5de-e423ec34a900","ValidationKey":"fbb28b32-f439-4801-a434-99c70aa388ca","Messages":{}}}}';
//$j=json_decode($json);
$transkey=$j->CreateTransactionResponse->CreateTransactionResult->TransportKey;
$vkey=$j->CreateTransactionResponse->CreateTransactionResult->ValidationKey;
echo $transkey."</br>";
echo $vkey."<br/>";
/*result as said:
aa900d54-7bfb-47e9-a5de-e423ec34a900
fbb28b32-f439-4801-a434-99c70aa388ca
*/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.