dptn69182 2019-02-18 16:54
浏览 29
已采纳

关于basicCard响应格式化文本的转义语句未解析

I have a php script on my website and this action is linked as a webhook to it. So once the data is submitted to the server, this is the response I send in 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  \
Remarks: McDonalds  \
Date: 2019-02-17T12:00:00+05:30  \
Amount: 2098  \
Category: Expense  \
If the details above aren't right, please inform the creator."}}]}}}}'

But none of the get parsed. What I actually entered in php was a double space followed by single backslash and then n but for some reason php adds an extra backslash preventing it from escaping even when I used json encode json_unescaped_slashes. Here is the php code I used to create the json.

            $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."  
Remarks: ".$item."  
Date: ".$date."  
Amount: ".$amount."  
Category: ".$category."  
If 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;

And this is the link to the screenshot of how it appears in the basicCard response https://photos.app.goo.gl/RzG5H3VgTJfrgfB59

  • 写回答

1条回答 默认 最新

  • dongyi7513 2019-02-19 12:08
    关注

    The problem is that you're encoding the object as JSON as an intermediary step, rather than building the object first in PHP and then encoding it. As such, you're getting some strange double-encoding issues.

    Doing something like this

      $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."  
    Remarks: ".$item."  
    Date: ".$date."  
    Amount: ".$amount."  
    Category: ".$category."  
    If 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 );
    

    is more like what you're trying to do.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 AttributeError: 'NoneType' object has no attribute 'drop_duplicates'报错
  • ¥15 编写c++计算器,要求如下
  • ¥15 以下代码,运行结果报错
  • ¥15 Vivado仿真数据出错
  • ¥15 银河麒麟不支持vant等前端框架怎么办?
  • ¥15 vue3的子组件在父页面调用不显示不生效问题
  • ¥15 cadence PEX
  • ¥15 phython创建了文件 显示在项目下面,但运行不了,运行时还是运行main文件,并且说main文件不允许并行运行
  • ¥20 C++哈希表的设计构造哈希表
  • ¥15 FutureWarning:不推荐使用空或全 NA 条目的 DataFrame 串联行为。怎么改呢?