我想使用PHP为text
JSON数组内的text
对象内的fulfillmentMessages
数组设置字符串值。
我目前正在尝试使用此代码,但失败了。
$response->fulfillmentMessages[text]->text = "Hello ";
echo json_encode($response);
与此相关的JSON:
{
"responseId": "b19f0045-830e-4d12-a57e-07cdfd55f",
"queryResult": {
"queryText": "yes",
"action": "send.first.question",
"parameters": {},
"allRequiredParamsPresent": true,
"fulfillmentMessages": [
{
"text": {
"text": [
""
]
}
}
],
"outputContexts": [
{
"name": "projects/quizbot-90a24/agent/sessions/9c08dfd274-7ece-19fe-c984-f5cd5ef135c7/contexts/expressname-followup",
"lifespanCount": 1,
"parameters": {
"userName": "saliya",
"userName.original": "saliya"
}
},
{
"name": "projects/quizbot-90a24/agent/sessions/9c08fdvfsd4-7ece-19fe-c984-f5cd5ef135c7/contexts/sessionusername",
"lifespanCount": 4,
"parameters": {
"userName": "saliya",
"userName.original": "saliya"
}
}
],
"intent": {
"name": "projects/quizbot-90agd24/agent/intents/063ed465-0ca1-4d94-bf7c-5b7ac4c31f6f",
"displayName": "expressName - yes"
},
"intentDetectionConfidence": 1,
"diagnosticInfo": {
"end_conversation": true
},
"languageCode": "en"
}
}
这是我打算采取的结果:
"fulfillmentMessages": [
{
"text": {
"text": [
"Hello"
]
}
}
答案 0 :(得分:1)
您的缩进让我呆了一秒钟。这将起作用
$response->queryResult->fulfillmentMessages[0]->text->text[0] = "Hello";
我没有注意到fulfillmentMessages
是queryResult
的财产。
答案 1 :(得分:0)
您正试图在嵌套对象和数组中操作数组项,因此需要将其更改为。
$response->queryResult->fulfillmentMessages[0]->text->text[0] = "Hello";
示例:
<?php
$response = json_decode('{
"responseId": "b19f0045-830e-4d12-a57e-07cdfd55f",
"queryResult": {
"queryText": "yes",
"action": "send.first.question",
"parameters": {},
"allRequiredParamsPresent": true,
"fulfillmentMessages": [{
"text": {
"text": [
""
]
}
}]
}
}');
$response->queryResult->fulfillmentMessages[0]->text->text[0] = "Hello";
print_r($response);
结果:
stdClass Object
(
[responseId] => b19f0045-830e-4d12-a57e-07cdfd55f
[queryResult] => stdClass Object
(
[queryText] => yes
[action] => send.first.question
[parameters] => stdClass Object
(
)
[allRequiredParamsPresent] => 1
[fulfillmentMessages] => Array
(
[0] => stdClass Object
(
[text] => stdClass Object
(
[text] => Array
(
[0] => Hello
)
)
)
)
)
)