我正在以JSON格式获得以下响应,我希望它将其转换为PHP变量。
JSON:
{"CreateTransactionResponse":{"CreateTransactionResult":{"TransportKey":"aa900d54-7bfb-47e9-a5de-e423ec34a900"
,"ValidationKey":"fbb28b32-f439-4801-a434-99c70aa388ca","Messages":{}}}}
输出应为 PHP:
$transkey = aa900d54-7bfb-47e9-a5de-e423ec34a900;
$vkey = fbb28b32-f439-4801-a434-99c70aa388ca
请告诉我如何做。
答案 0 :(得分:0)
如果你想访问你的json,请先尝试解码它:
$result = json_decode($yourJSON, true);
foreach($result['CreateTransactionResponse'] as $key => $val){
echo $transkey = 'TransportKey= ' . $val['TransportKey'] . '<br/>;
echo $vkey = 'ValidationKey= ' . $val['ValidationKey'];
}
或者它是否是JSON的数组
$result = json_decode($yourJSON, true);
$data = [];
foreach($result['CreateTransactionResponse'] as $key => $val){
$data[] = [
'TransportKey' => $val['TransportKey'],
'ValidationKey' => $val['ValidationKey']
];
}
print_r($data);
答案 1 :(得分:0)
只需使用json_decode();
from __future__ import unicode_literals
import webbrowser
from bs4 import BeautifulSoup
import requests
RANDOM_WIKI_URL = "https://en.wikipedia.org/wiki/Special:Random"
def get_user_input():
user_input = ''
while user_input not in ('a', 'b', 'q'):
print '-' * 79
print "(a): Open in new browser tab"
print "(b): Get new random article"
print "(q): Quit"
print '-' * 79
user_input = raw_input("[a|b|q]: ").lower()
return user_input
def main():
while True:
print "=" * 79
print "Retrieving random wikipedia article..."
response = requests.get(RANDOM_WIKI_URL)
data = response.content
url = response.url
soup = BeautifulSoup(data, 'html.parser')
title = soup.select('#firstHeading')[0].get_text()
print "Random Wikipedia article: '{}'".format(title)
user_input = get_user_input()
if user_input == 'q':
break
elif user_input == 'a':
webbrowser.open_new_tab(url)
if __name__ == '__main__':
main()
答案 2 :(得分:0)
json到数组(json_decode
),然后是数组中的extract
。
$arr = json_decode($json, true);
extract($arr);
var_dump($CreateTransactionResponse);
输出:
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
有关extract
的更多信息使用$CreateTransactionResult['TransportKey']
从JSON访问传输密钥。类似地,$CreateTransactionResult['ValidationKey']
用于验证密钥。
答案 3 :(得分:0)
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>';
}
}
}
答案 4 :(得分:0)
在这种情况下,如果一次只有一个TransportKey
和一个ValidationKey
值(不是数组/对象),这是最简单的 。否则,如果对象包含我们想要使用的对象或内部对象或转换为变量,则应使用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
*/