Remove double quote in json_encode ()

I want to remove the double quote in my json_encode, i.e. 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:

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

I want to remove the double quote "id_posiciones" and "device_version".

How can I do for the result:

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

If you add an underscore to your regular expression at the end, it will do it.

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

I guess this is preg_replace anyway.

+4
source

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

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

It will work> = PHP 5.3.

 $encoded = json_encode($data, JSON_NUMERIC_CHECK); 
+16
source

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 class regex [a-zA-Z] does not match '_'

+5
source
 // use can use addslashes() function for storing in mysql database // or remove slashes u can use stripslashes() function. $json_array = array( 'title' => 'Example string\ with "special" characters' ); echo $json_decode =addslashes(json_encode($json_array)); output-{\"title\":\"Example string\ with \\\"special\\\" characters\"} 
+2
source

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

+1
source

All Articles