php 如何使用php删除json响应中的反斜杠(“\”)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14209052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:51:58  来源:igfitidea点击:

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

phpmysqljson

提问by user1957012

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

我尝试将一行 mysql 查询添加到 JSON whit php 中。我使用这个代码:

public function lugaresCercanos($lng, $lat, $distance){
$result=mysql_query("SELECT nombre, distancia FROM Lugar ORDER BY distancia ASC");
$info=array();
    while($row = mysql_fetch_array($result,MYSQL_ASSOC)){
        array_push($info,$row);
    }
    return json_encode($info);

This returns a JSONObject, but I'm not sure.

这将返回一个 JSONObject,但我不确定。

    class resultado_final {
public $logstatus = "";
public $lugares_cercanos = "";}

$result_final = new resultado_final();
if($db->login($usuario,$passw)){
$result_final->logstatus = "0";}else{
$result_final->logstatus = "1";}
$result_final->lugares_cercanos = $lista;
echo json_encode($result_final);

This code print this:

此代码打印此:

{"logstatus":"1","lugares_cercanos":"[{\"nombre\":\"Rio Amazonas\",\"distancia\":\"5119.000\"},{\"nombre\":\"Swissotel \",\"distancia\":\"5823.000\"},{\"nombre\":\"Laguna de Yaguarcocha\",\"distancia\":\"71797.000\"}]"}

why the rows of the query are separated by backslashes? how remove the backslashes? Thanks alot!

为什么查询的行用反斜杠分隔?如何删除反斜杠?非常感谢!

回答by Nick Perkins

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

\ 用于转义作为响应一部分的引号 (")。

Use stripslashes()to strip these out.

使用stripslashes()剥离出来这些。

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

当包含在引号中的字符串包含引号时,必须对其进行转义。php 中的转义字符是\.

回答by sk8terboi87 ツ

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

(如果 unescape 不起作用) http://php.net/manual/en/function.json-encode.php

回答by Ignacio Vazquez-Abrams

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

停止对您的数据进行双重编码。将所有内容放在一个大型结构中,然后仅对其进行编码。

回答by user1957012

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"}]}

回答by Devner

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

如果有人想删除反斜杠并漂亮地打印 JSON 数据,可以使用以下内容:

json_encode($my_array, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);