简体   繁体   中英

Remove double quotes from json array and fix link

Hi i have this code to output a array list

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

this output

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

i need to remove double quotes from keys and fix the http link to looks like this

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

Thanks

Try this..I know it is not very good way to do it.. I am writing this to show you that you have to make the form that you wanted in your php code only.

$json = json_encode($musicas);
$json = preg_replace('/["]/', '' ,$json);
$json = str_replace(':',':"', $json);
$json = str_replace(',','",',$json);
$json = str_replace('}]','"}]',$json);
echo $json;

By this way you will acheive what you wanted. But please find something good way to do it.

The correct answer should be this, as commented by @AlvaroLouzada

$json = json_encode($musicas);
$json = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', $json);
echo $json ;

I looked a lot for an elegant solution to fix this issue without doing changing things over javascript or just replace quotes via preg_replace (for the case that the values would contain quotes) and end up doing it by myself. even if it's too late, I hope it would help those who are looking for the same solution.

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

usage:

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

result:

{
    someField : "value",
    labelField : "label",
    boolean : false,
    numeric : 5,
    render : {
        option : function() {
            console.log("Hello World!");
            console.log('Hello World!');
        }
    }
}

Instead of doing echo ( json_encode($musicas) ); , do something like this:

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

echo $json_string;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM