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 python天天向上类似问题,但没有清零
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能