0

嗨我有这个代码来输出一个数组列表

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

谢谢

4

3 回答 3

3

试试这个..我知道这不是很好的方法..我写这个是为了告诉你,你必须只在你的 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 ;
于 2013-02-14T18:51:01.883 回答
1

我一直在寻找一个优雅的解决方案来解决这个问题,而无需通过 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);
}

用法:

$array = [
    'someField' => '"value"', // double quotes for string if needed
    'labelField' => '"label"', // double quotes for string if needed
    'boolean' => false,
    'numeric' => 5,
    'render' => [
        'option' => 'function() {
            console.log("Hello World!");
            console.log(\'Hello World!\');
        }',
    ],
];
echo json_encode_advanced($array);

结果:

{
    someField : "value",
    labelField : "label",
    boolean : false,
    numeric : 5,
    render : {
        option : function() {
            console.log("Hello World!");
            console.log('Hello World!');
        }
    }
}
于 2017-03-15T19:40:59.030 回答
0

而不是做echo ( json_encode($musicas) );,做这样的事情:

$json_string = json_encode($musicas);
$json_string = str_replace('\/', '/', $json_string);

echo $json_string;
于 2013-02-14T18:27:56.213 回答