在PHP中获取JSON查询的值
您好,我正在尝试将在PHP中获取JSON查询的值,php,json,Php,Json,您好,我正在尝试将town_B的值分配给PHP中的一个变量 我尝试使用下面的方法,但没有成功。如果您有任何建议,我们将不胜感激 $varx = $update["result"]["parameters"]["town_two"]; 下面是JSON响应 { "id": "86726c74-cb52-4f1e-983d-1bc68d8c4f9c", "timestamp": "2018-02-22T18:03:12.964Z", "lang": "en", "result": {
town_B
的值分配给PHP中的一个变量
我尝试使用下面的方法,但没有成功。如果您有任何建议,我们将不胜感激
$varx = $update["result"]["parameters"]["town_two"];
下面是JSON响应
{
"id": "86726c74-cb52-4f1e-983d-1bc68d8c4f9c",
"timestamp": "2018-02-22T18:03:12.964Z",
"lang": "en",
"result": {
"source": "agent",
"resolvedQuery": "Disneyworld CA",
"action": "sayHello",
"actionIncomplete": false,
"parameters": {
"town_B": [
"CA"
],
"town_A": "Disneyworld"
},
"contexts": [],
"metadata": {
"intentId": "65bc2f1f-e127-44de-bd3b-915c8865f472",
"webhookUsed": "true",
"webhookForSlotFillingUsed": "false",
"webhookResponseTime": 1047,
"intentName": "Geo"
},
"fulfillment": {
"source": "agent",
"messages": [
{
"type": 0,
"speech": "Please check for correct input"
}
]
},
"score": 1
},
"status": {
"code": 200,
"errorType": "success",
"webhookTimedOut": false
},
"sessionId": "c3de9b17-6cd6-43dc-bf12-6844a6b0930e"
}
我从未见过两个城市,可能是城市
$json = '{
"id": "86726c74-cb52-4f1e-983d-1bc68d8c4f9c",
"timestamp": "2018-02-22T18:03:12.964Z",
"lang": "en",
"result": {
"source": "agent",
"resolvedSomething is wrong": "Disneyworld CA",
"action": "sayHello",
"actionIncomplete": false,
"parameters": {
"town_B": [
"CA"
],
"town_A": "Disneyworld"
},
"contexts": [],
"metadata": {
"intentId": "65bc2f1f-e127-44de-bd3b-915c8865f472",
"webhookUsed": "true",
"webhookForSlotFillingUsed": "false",
"webhookResponseTime": 1047,
"intentName": "Geo"
},
"fulfillment": {
"source": "agent",
"messages": [
{
"type": 0,
"speech": "Please check for correct input"
}
]
},
"score": 1
},
"status": {
"code": 200,
"errorType": "success",
"webhookTimedOut": false
},
"Something is wrongId": "c3de9b17-6cd6-43dc-bf12-6844a6b0930e"
}';
$update = json_decode($json, true);
echo $update["result"]["parameters"]["town_B"][0];
说明:
您需要使用json\u decode
来解码json
更新:
$update = json_decode($json, true);
echo $update["result"]["parameters"]["town_B"][0];
town_B是一个数组,因此对于输出“CA”,需要选择第一个元素
town\u two
从未定义过-town\u A
而town\u B
则是。还要确保首先对原始json字符串调用了json_decode($json,true)
,以便将其转换为PHP数组代码>此方法仅返回town_B示例中值的第一个字符,如果town_B为“Hello”,则返回H而不是Hello@Krishno,这不是真的。。。它将是town_B Array上的返回第一个元素在您的json中,您有“town_B”:[“CA”],如果它在@iamPacMan工作,您能接受答案吗