删除json_encode()中的双引号 [英] Remove double quote in json_encode()
本文介绍了删除json_encode()中的双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想删除我的json_encode中的双引号,即我的代码:
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 '[]';
}
?>
我的结果是:
[{"id_posiciones":"1",posicion:"1",nick:"biwer",puntos:"1000",uid:"1",pais:"ES",idioma:"ES","device_version":"4"}]
我也想删除"id_posiciones"和"device_version"的双引号.
I want to remove double quote of "id_posiciones" and "device_version" too.
我该怎么做呢?
[{id_posiciones:"1",posicion:"1",nick:"biwer",puntos:"1000",uid:"1",pais:"ES",idioma:"ES",device_version:"4"}]
推荐答案
如果在最后的正则表达式中添加下划线,它将成功.
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);
我认为这正是preg_replace的目的.
I assume that's what that preg_replace is for anyway.
这篇关于删除json_encode()中的双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文