php 删除 json_encode() 中的双引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13938645/
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:18:31  来源:igfitidea点击:

Remove double quote in json_encode()

phppreg-replacejsondouble-quotes

提问by jordiAnd

I want remove double quote in my json_encode, that is my code:

我想在我的 json_encode 中删除双引号,这是我的代码:

<?php

require_once 'config.inc.php';
//## Clase Base de Datos
require_once 'Database.class.php';
//## Obtengo los parametros pasados por el metodo GET
$params = $_REQUEST;

$db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
$db->connect();

$result = mysql_query("SELECT * from ranking WHERE posicion BETWEEN     ".$params['pos_ini']." AND ".$params['pos_fi']) or die('Could not query');

if(mysql_num_rows($result)){
    $array_json=array();
    $filas = mysql_num_rows($result);
    $columnas = mysql_num_fields($result);

    for($i=0;$i<$filas;$i++)
    {
        $fila_dato = mysql_fetch_assoc($result);
        for($k=0;$k<$columnas;$k++)
        {
                    $campo = mysql_field_name($result,$k);
                    $campo = str_replace('\"', '', $campo);
                    $array_json[$i][$campo] = $fila_dato[$campo];
        }
    }
    $array_final = json_encode($array_json);
    $array_final = preg_replace('/"([a-zA-Z]+[a-zA-Z0-9]*)":/',':',$array_final);
    echo $array_final;
} else {
    echo '[]';
}
?>

My result is that:

我的结果是:

[{"id_posiciones":"1",posicion:"1",nick:"biwer",puntos:"1000",uid:"1",pais:"ES",idioma:"ES","device_version":"4"}]

I want to remove double quote of "id_posiciones" and "device_version" too.

我也想删除“id_posiciones”和“device_version”的双引号。

How can I do for the result is that:

我该怎么做才能得到结果:

[{id_posiciones:"1",posicion:"1",nick:"biwer",puntos:"1000",uid:"1",pais:"ES",idioma:"ES",device_version:"4"}]

采纳答案by Alex

If you add an underscore to your regex at the end it will do it.

如果最后在正则表达式中添加下划线,它就会这样做。

$array_final = preg_replace('/"([a-zA-Z]+[a-zA-Z0-9_]*)":/',':',$array_final);

I assume that's what that preg_replace is for anyway.

我认为这就是 preg_replace 的用途。

回答by Vijaysinh Parmar

You can use this bellow code to remove quote from numeric value.

您可以使用此波纹管代码从数值中删除引号。

http://php.net/manual/en/json.constants.php

http://php.net/manual/en/json.constants.php

It will work >=PHP 5.3.

它将工作>=PHP 5.3。

$encoded = json_encode($data, JSON_NUMERIC_CHECK);

回答by hek2mgl

Replace this line:

替换这一行:

 $array_final = preg_replace('/"([a-zA-Z]+[a-zA-Z0-9]*)":/',':',$array_final);

by:

经过:

$array_final = preg_replace('/"([a-zA-Z_]+[a-zA-Z0-9_]*)":/',':',$array_final);

Note that the regex class [a-zA-Z] does not match the '_'

请注意,正则表达式类 [a-zA-Z] 与“_”不匹配

回答by Ghostman

You can use $.parseJSONto parse the string and create a Javascript object from it, or better yet use a method like $.getJSONto get it

您可以使用$.parseJSON来解析字符串并从中创建一个 Javascript 对象,或者更好地使用类似的方法$.getJSON来获取它

回答by Manmeet Khurana

// use can use addslashes() function for storing in mysql database 
// or remove  slashes  u can use stripslashes() function.

$json_array = array(
'title' => 'Example string\'s with "special" characters'
);

echo $json_decode =addslashes(json_encode($json_array));


output-{\"title\":\"Example string\'s with \\"special\\" characters\"}