basicCard 响应格式化文本上的转义语句未解析
Escape statements on basicCard response formatted text not parsed
我的网站上有一个 php 脚本,此操作已 link 编辑为它的网络挂钩。所以一旦数据提交到服务器,这就是我在 json.
中发送的响应
`{"payload":
{"google":
{"expectUserResponse":true,"
richResponse":
{"items":[{
"simpleResponse":{
"textToSpeech":"This feature is coming soon! Please wait until you receive a notification about its launch! You can verify if the details collected for your entry were right and inform the creator if they weren't. Here are the details."
}
},{"basicCard":{"title":"This is the entry I made","subtitle":"But I couldn't submit it","formattedText":"Item: Books \nRemarks: McDonalds \nDate: 2019-02-17T12:00:00+05:30 \nAmount: 2098 \nCategory: Expense \nIf the details above aren't right, please inform the creator."}}]}}}}'
但是 \n 的 none 被解析了。我在 php 中实际输入的是双 space 后跟单反斜杠然后是 n 但由于某种原因 php 添加了一个额外的反斜杠,即使我使用 [=24= 也可以防止它转义]编码json_unescaped_slashes。这是我用来创建 json.
的 php 代码
$response=new \stdClass();
$response->payload->google->expectUserResponse= true;
$items=new \stdClass();
$res->simpleResponse->textToSpeech="This feature is coming soon! Please wait until you receive a notification about its launch! You can verify if the details collected for your entry were right and inform the creator if they weren't. Here are the details.";
$items->basicCard->title="This is the entry I made";
$items->basicCard->subtitle="But I couldn't submit it";
$items->basicCard->formattedText="Item: ".$type." \nRemarks: ".$item." \nDate: ".$date." \nAmount: ".$amount." \nCategory: ".$category." \nIf the details above aren't right, please inform the creator.";
$response->payload->google->richResponse->items[]=json_encode($res, JSON_UNESCAPED_SLASHES).",".json_encode($items, JSON_UNESCAPED_SLASHES);
$response2=str_replace('\"','"',json_encode($response, JSON_UNESCAPED_SLASHES));
$response2=str_replace('"{','{',$response2);
$response2=str_replace('}"','}',$response2);
echo $response2;
这是 link 显示在 basicCard 响应中的屏幕截图
https://photos.app.goo.gl/RzG5H3VgTJfrgfB59
问题是您将对象编码为 JSON 作为中间步骤,而不是先在 PHP 中构建对象然后对其进行编码。因此,您遇到了一些奇怪的双重编码问题。
做这样的事情
$msg=new \stdClass();
$msg->simpleResponse->textToSpeech="Coming soon";
$card=new \stdClass();
$card->basicCard->title="This is the entry I made";
$card->basicCard->subtitle="But I couldn't submit it";
$card->basicCard->formattedText="Item: ".$type." \nRemarks: ".$item." \nDate: ".$date." \nAmount: ".$amount." \nCategory: ".$category." \nIf the details above aren't right, please inform the creator.";
$response=new \stdClass();
$response->payload->google->expectUserResponse= true;
$response->payload->google->richResponse->items = array(
$msg,
$card
);
$json = json_encode( $response );
更像是你想要做的。
我的网站上有一个 php 脚本,此操作已 link 编辑为它的网络挂钩。所以一旦数据提交到服务器,这就是我在 json.
中发送的响应`{"payload":
{"google":
{"expectUserResponse":true,"
richResponse":
{"items":[{
"simpleResponse":{
"textToSpeech":"This feature is coming soon! Please wait until you receive a notification about its launch! You can verify if the details collected for your entry were right and inform the creator if they weren't. Here are the details."
}
},{"basicCard":{"title":"This is the entry I made","subtitle":"But I couldn't submit it","formattedText":"Item: Books \nRemarks: McDonalds \nDate: 2019-02-17T12:00:00+05:30 \nAmount: 2098 \nCategory: Expense \nIf the details above aren't right, please inform the creator."}}]}}}}'
但是 \n 的 none 被解析了。我在 php 中实际输入的是双 space 后跟单反斜杠然后是 n 但由于某种原因 php 添加了一个额外的反斜杠,即使我使用 [=24= 也可以防止它转义]编码json_unescaped_slashes。这是我用来创建 json.
的 php 代码 $response=new \stdClass();
$response->payload->google->expectUserResponse= true;
$items=new \stdClass();
$res->simpleResponse->textToSpeech="This feature is coming soon! Please wait until you receive a notification about its launch! You can verify if the details collected for your entry were right and inform the creator if they weren't. Here are the details.";
$items->basicCard->title="This is the entry I made";
$items->basicCard->subtitle="But I couldn't submit it";
$items->basicCard->formattedText="Item: ".$type." \nRemarks: ".$item." \nDate: ".$date." \nAmount: ".$amount." \nCategory: ".$category." \nIf the details above aren't right, please inform the creator.";
$response->payload->google->richResponse->items[]=json_encode($res, JSON_UNESCAPED_SLASHES).",".json_encode($items, JSON_UNESCAPED_SLASHES);
$response2=str_replace('\"','"',json_encode($response, JSON_UNESCAPED_SLASHES));
$response2=str_replace('"{','{',$response2);
$response2=str_replace('}"','}',$response2);
echo $response2;
这是 link 显示在 basicCard 响应中的屏幕截图 https://photos.app.goo.gl/RzG5H3VgTJfrgfB59
问题是您将对象编码为 JSON 作为中间步骤,而不是先在 PHP 中构建对象然后对其进行编码。因此,您遇到了一些奇怪的双重编码问题。
做这样的事情
$msg=new \stdClass();
$msg->simpleResponse->textToSpeech="Coming soon";
$card=new \stdClass();
$card->basicCard->title="This is the entry I made";
$card->basicCard->subtitle="But I couldn't submit it";
$card->basicCard->formattedText="Item: ".$type." \nRemarks: ".$item." \nDate: ".$date." \nAmount: ".$amount." \nCategory: ".$category." \nIf the details above aren't right, please inform the creator.";
$response=new \stdClass();
$response->payload->google->expectUserResponse= true;
$response->payload->google->richResponse->items = array(
$msg,
$card
);
$json = json_encode( $response );
更像是你想要做的。