您好,我正在尝试将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"
}
答案 0 :(得分:0)
我从未见过town_two
,可能是town_B
$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_decode
解码json http://php.net/manual/en/function.json-decode.php
更新:
$update = json_decode($json, true);
echo $update["result"]["parameters"]["town_B"][0];
town_B是一个数组,所以对于输出“CA”,需要选择第一个元素。