7

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

<?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]*)":/','$1:',$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.

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"}]
jordiAnd
  • 127
  • 1
  • 1
  • 8

6 Answers6

34

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

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

It will work >=PHP 5.3.

$encoded = json_encode($data, JSON_NUMERIC_CHECK);
Vijaysinh Parmar
  • 897
  • 1
  • 15
  • 19
  • 1
    Please note that this will remove the quotes from the json values only and not the keys. – Shadi Jan 05 '18 at 16:49
  • 1
    This is not what the original question is about, but it is very useful and it is what I was looking for, thanks! – Paul Feakins Nov 15 '18 at 18:51
  • 2
    @Paul for that reason, researchers MUST NOT upvote answers (even if helpful) that do not correctly answer the question being asked! There are many pages on Stack Overflow that are plagued by highly upvoted incorrect answers. These answers mislead, confuse, and waste the time of researchers. Volunteer content curators cannot delete these answers because they have acquired too many upvotes. This IS a problem for Stack Overflow. – mickmackusa Nov 20 '20 at 04:04
9

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_]*)":/','$1:',$array_final);

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

Alex
  • 219
  • 1
  • 4
  • Do not manually hack json strings, even if it _works_ there is a more professional way. – mickmackusa Nov 20 '20 at 04:05
  • I didn't hack anything, I just fixed his regex :) It's not even JSON once he takes the quotes out, so I assumed he had a reason to do it. Anyway, what's the reason not to and what is the more professional way? – Alex Nov 21 '20 at 05:58
  • I don't code for Android so I don't know why this is necessary. Using string functions to mutate a json string is not recommended because there may be unintended consequences. The pro way is to mutate the decoded data. I find this question to be odd. Perhaps artragis's solution is more ideal. – mickmackusa Nov 21 '20 at 06:20
6

Replace this line:

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

by:

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

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

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
3
// 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\"}
Manmeet Khurana
  • 367
  • 2
  • 13
1

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

Ghostman
  • 6,042
  • 9
  • 34
  • 53
0

For double quotes you can use &quot;

For single quotes &apos;

$string=str_replace('"',"&quot;",$string);
Vitalicus
  • 1,188
  • 13
  • 15