[英]Set values to a JSON object array in PHP
I want to set a string value to the text
array inside the text
object which is inside the fulfillmentMessages
JSON array using PHP. 我想使用PHP将一个字符串值设置为
text
对象内部的text
数组,该text
对象位于fulfillmentMessages
JSON数组内部。 I am currently trying with this code, but have failed. 我目前正在尝试使用此代码,但失败了。
$response->fulfillmentMessages[text]->text = "Hello ";
echo json_encode($response);
The JSON I am getting with this: 我得到的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"
}
}
This is the result I intend to take: 这是我打算采取的结果:
"fulfillmentMessages": [
{
"text": {
"text": [
"Hello"
]
}
}
your indentation threw me for a second. 你的缩进让我一秒钟。 this will work
这会工作
$response->queryResult->fulfillmentMessages[0]->text->text[0] = "Hello";
I didnt notice that fulfillmentMessages
was a property of queryResult
. 我没有注意到,
fulfillmentMessages
是queryResult
一个属性。
You're trying to manipulate an array item in nested object and array, so you need to change it to. 您试图操纵嵌套对象和数组中的数组项,因此需要将其更改为。
$response->queryResult->fulfillmentMessages[0]->text->text[0] = "Hello";
Example: 例:
<?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);
Result: 结果:
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
)
)
)
)
)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.