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 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd