Remove double quote in json_encode()

匿名 (未验证) 提交于 2019-12-03 02:26:02

问题:

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

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 

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

回答1:

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.



回答2:

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); 


回答3:

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 '_'



回答4:

// 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\"} 


回答5:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!