dt2015 2016-08-14 09:18
浏览 138

将json打印到字符串

I have to write json result in a string.

Here is my code,

<!DOCTYPE html> <html lang="en">
     <head>
         <meta charset="UTF-8">
         <title></title>
     </head>
     <body>
         <form method="POST">
             Enter Pin <input type="text" name="pinCode">
             <input type="submit" name="formSubmit">
         </form>
     </body> </html>


 <?php

     if(isset($_POST['formSubmit']))
     {
         $input = $_POST['pinCode'];
         $shortUrl=file_get_contents("https://www.whizapi.com/api/v2/util/uiin/indian-city-by-postal-code?project-app-key=<app_key>&pin=".$input);
         $res = json_decode($shortUrl, true);

         echo implode($res);
     }

 ?>

Currently the outcome is on json format. I have to print the result in string. e.g - { "title" : "Mr", "name" : "sandeep"}. Result would be like "Mr sandeep". That's why I have used json_decode for changing json into array but then I couldn't understand how to change associative array in string.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dsapkqaduj6718493 2016-08-14 09:21
    关注

    Don't decode the json if you want it in string. The output is string and you are converting it to array using json_decode so just comment that line

    if(isset($_POST['formSubmit']))
    {
        $input = $_POST['pinCode'];
        $shortUrl=file_get_contents("https://www.whizapi.com/api/v2/util/ui/in/indian-city-by-postal-code?project-app-key=<app_key>&pin=".$input);
        //$res = json_decode($shortUrl, true);
    
        echo $shortUrl;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集