2016-08-14 193 views
1

我必须在字符串中写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”}。结果会像“先生sandeep”。这就是为什么我使用json_decode将json更改为数组,但后来我无法理解如何更改字符串中的关联数组。

在此先感谢。

+0

json导致字符串?你实际上想要达到什么。根据tat的输入提供一些输入数据和预期结果,以使我们对自己清楚 –

+0

目前的结果是json格式。我必须以字符串打印结果。例如 - {“title”:“Mr”,“name”:“sandeep”}。结果会像“先生sandeep” –

回答

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

感谢您的答复,但目前的结果是在JSON格式。我必须以字符串打印结果。例如 - {“title”:“Mr”,“name”:“sandeep”}。结果会像“先生sandeep”。这就是为什么我使用json_endcode来更改数组中的json,然后我不明白如何在字符串中打印关联数组。 –

+0

可以提供json格式和所需的输出 –