将json打印到字符串

时间:2016-08-14 09:18:16

标签: php html json

我必须在字符串中写入json结果。

这是我的代码,

<!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);
     }

 ?>

目前结果是json格式。我必须在字符串中打印结果。例如 - {“title”:“Mr”,“name”:“sandeep”}。结果就像“桑迪普先生”。这就是为什么我使用json_decode将json更改为数组然后我无法理解如何更改字符串中的关联数组。

提前致谢。

1 个答案:

答案 0 :(得分:1)

如果你想要它在字符串中,请不要解码json。输出是字符串,您使用json_decode将其转换为数组,因此只需注释该行

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;
}