Php 从json数组中删除双引号并修复链接
嗨,我有这个代码来输出数组列表Php 从json数组中删除双引号并修复链接,php,arrays,json,multidimensional-array,Php,Arrays,Json,Multidimensional Array,嗨,我有这个代码来输出数组列表 $Sql1 = "SELECT * FROM tabmp3 WHERE Mp3_Player = 1 "; $Query1 = mysql_query($Sql1, $Conn)or die(mysql_error($Conn)); $musicas = array(); while($Rs1 = mysql_fetch_array($Query1)){ $mus
$Sql1 = "SELECT * FROM tabmp3
WHERE Mp3_Player = 1
";
$Query1 = mysql_query($Sql1, $Conn)or die(mysql_error($Conn));
$musicas = array();
while($Rs1 = mysql_fetch_array($Query1)){
$musicas[] = array( title => $Rs1['Musica_Nome'], autor => "Grupo Fronteiras", mp3 => "http://site/Musicas/".$Rs1["Disco_Id"]."/".$Rs1["Musica_Mp3"] );
}
echo ( json_encode($musicas) );
这个输出
[{"title":"Alegria do Pov\u00e3o","autor":"Grupo Fronteiras","mp3":"http:\/\/site\/Musicas\/3\/201302140204413c390efdb9957eebd8d85c262f2a4929.mp3"}, {"title":"Bem na moda da fronteira","autor":"Grupo Fronteiras","mp3":"http:\/\/site\/Musicas\/2\/20130214032235fabd12471ffc7790c9204f891919bca8.mp3"}]
我需要从键中删除双引号,并修复http链接,使其看起来像这样
[{title:"Alegria do Pov\u00e3o",autor:"Grupo Fronteiras",mp3:"http://site/Musicas/3/201302140204413c390efdb9957eebd8d85c262f2a4929.mp3"},{title:"Bem na moda da fronteira",autor:"Grupo Fronteiras",mp3:"http://site/Musicas/2/20130214032235fabd12471ffc7790c9204f891919bca8.mp3"}]
谢谢你,不用做echo(json_encode($musicas))代码>,执行如下操作:
$json_string = json_encode($musicas);
$json_string = str_replace('\/', '/', $json_string);
echo $json_string;
而不是执行echo(json_encode($musicas))代码>,执行如下操作:
$json_string = json_encode($musicas);
$json_string = str_replace('\/', '/', $json_string);
echo $json_string;
试试这个我知道这不是很好的方法。。我写这篇文章是为了向您展示,您必须只在php代码中创建所需的表单
$json = json_encode($musicas);
$json = preg_replace('/["]/', '' ,$json);
$json = str_replace(':',':"', $json);
$json = str_replace(',','",',$json);
$json = str_replace('}]','"}]',$json);
echo $json;
这样你就会得到你想要的。但是请找到一些好的方法来做
The correct answer should be this, as commented by @AlvaroLouzada
$json = json_encode($musicas);
$json = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', $json);
echo $json ;
试试这个我知道这不是很好的方法。。我写这篇文章是为了向您展示,您必须只在php代码中创建所需的表单
$json = json_encode($musicas);
$json = preg_replace('/["]/', '' ,$json);
$json = str_replace(':',':"', $json);
$json = str_replace(',','",',$json);
$json = str_replace('}]','"}]',$json);
echo $json;
这样你就会得到你想要的。但是请找到一些好的方法来做
The correct answer should be this, as commented by @AlvaroLouzada
$json = json_encode($musicas);
$json = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', $json);
echo $json ;
我一直在寻找一个优雅的解决方案来解决这个问题,而不需要在javascript上做任何更改,也不需要通过preg_replace(对于值将包含引号的情况)来替换引号,最终由我自己完成。即使为时已晚,我希望它能帮助那些正在寻找相同解决方案的人
function json_encode_advanced(array $arr, $sequential_keys = false, $quotes = false, $beautiful_json = false) {
$output = "{";
$count = 0;
foreach ($arr as $key => $value) {
if ( isAssoc($arr) || (!isAssoc($arr) && $sequential_keys == true ) ) {
$output .= ($quotes ? '"' : '') . $key . ($quotes ? '"' : '') . ' : ';
}
if (is_array($value)) {
$output .= json_encode_advanced($value, $sequential_keys, $quotes, $beautiful_json);
} else if (is_bool($value)) {
$output .= ($value ? 'true' : 'false');
} else if (is_numeric($value)) {
$output .= $value;
} else {
$output .= ($quotes || $beautiful_json ? '"' : '') . $value . ($quotes || $beautiful_json ? '"' : '');
}
if (++$count < count($arr)) {
$output .= ', ';
}
}
$output .= "}";
return $output;
}
function isAssoc(array $arr) {
if (array() === $arr) return false;
return array_keys($arr) !== range(0, count($arr) - 1);
}
结果:
{
someField : "value",
labelField : "label",
boolean : false,
numeric : 5,
render : {
option : function() {
console.log("Hello World!");
console.log('Hello World!');
}
}
}
我一直在寻找一个优雅的解决方案来解决这个问题,而不需要在javascript上做任何更改,也不需要通过preg_replace(对于值将包含引号的情况)来替换引号,最终由我自己完成。即使为时已晚,我希望它能帮助那些正在寻找相同解决方案的人
function json_encode_advanced(array $arr, $sequential_keys = false, $quotes = false, $beautiful_json = false) {
$output = "{";
$count = 0;
foreach ($arr as $key => $value) {
if ( isAssoc($arr) || (!isAssoc($arr) && $sequential_keys == true ) ) {
$output .= ($quotes ? '"' : '') . $key . ($quotes ? '"' : '') . ' : ';
}
if (is_array($value)) {
$output .= json_encode_advanced($value, $sequential_keys, $quotes, $beautiful_json);
} else if (is_bool($value)) {
$output .= ($value ? 'true' : 'false');
} else if (is_numeric($value)) {
$output .= $value;
} else {
$output .= ($quotes || $beautiful_json ? '"' : '') . $value . ($quotes || $beautiful_json ? '"' : '');
}
if (++$count < count($arr)) {
$output .= ', ';
}
}
$output .= "}";
return $output;
}
function isAssoc(array $arr) {
if (array() === $arr) return false;
return array_keys($arr) !== range(0, count($arr) - 1);
}
结果:
{
someField : "value",
labelField : "label",
boolean : false,
numeric : 5,
render : {
option : function() {
console.log("Hello World!");
console.log('Hello World!');
}
}
}
为什么要删除它。。这是因为您使用了
json\u encode
而生成的输出。第一种形式是有效的json,这有什么原因不起作用吗?您可以使用修复URL,但不要删除引号,除非您喜欢破坏json编码。hi@Pankaj和我正在使用jPlayer播放MP3,播放机的格式类似于第二个示例。@AlvaroLouzada,因此不要直接回显编码形式。。等等,嗯,回答..为什么要删除它。。这是因为您使用了json\u encode
而生成的输出。第一种形式是有效的json,这有什么原因不起作用吗?您可以使用修复URL,但不要删除引号,除非您喜欢破坏json编码。hi@Pankaj和我正在使用jPlayer播放MP3,播放机的格式类似于第二个示例。@AlvaroLouzada,因此不要直接回显编码形式。。等等,嗯回答..Hi@Dugi这是删除斜杠,但是我如何删除键上的双引号?Hi@Dugi这是删除斜杠,但是我如何删除键上的双引号?$json_string=preg_replace(“/”([^”]+)“\s*:\s*/”,“$1:”,$json_string);@AlvaroLouzada是的,你是对的。实际上我在preg_replace工作了一周。:$json_string=preg_replace(“/”([^“]+)“\s*:\s*/”,“$1:”,$json_string)@是的,你是对的。事实上,我在preg_工作了一周。:)