how remove the backslash (“\”) in the json response using php?

前端 未结 5 679
伪装坚强ぢ
伪装坚强ぢ 2020-12-30 12:07

I try to add a row of mysql query into JSON whit php. I use this code:

public function lugaresCercanos($lng, $lat, $distance){
$result=mysql_query(\"SELECT n         


        
相关标签:
5条回答
  • 2020-12-30 12:40

    The \ is to escape the quotes (") that are part of the response.

    Use stripslashes() to strip these out.

    When a string wrapped in quotes contains quotes, they have to be escaped. The escape character in php is \.

    0 讨论(0)
  • 2020-12-30 12:50

    Try

    json_encode($arr, JSON_UNESCAPED_SLASHES);
    

    or

    echo str_replace('\/','/',json_encode($mydatas));
    

    (if unescape doesn't work) http://php.net/manual/en/function.json-encode.php

    0 讨论(0)
  • 2020-12-30 12:52

    Stop double-encoding your data. Put everything together in one large structure and then encode only that.

    0 讨论(0)
  • 2020-12-30 12:53

    Thanks, I resolve it.

    $info=array();
        while($row = mysql_fetch_array($result,MYSQL_ASSOC)){
            array_push($info,$row);
        }
        $info;
    $result_final->lugares_cercanos = $info;
    

    Print this:

    {"logstatus":"1","lugares_cercanos":[{"nombre":"Rio Amazonas","distancia":"5119.000"}{"nombre":"Swissotel Quito","distancia":"5823.000"}{"nombre":"Laguna de Yaguarcocha","distancia":"71797.000"}]}
    
    0 讨论(0)
  • 2020-12-30 13:07

    If anyone wants to remove the backslashes and also pretty print the JSON data, the following can be used:

    json_encode($my_array, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
    
    0 讨论(0)
提交回复
热议问题