简体   繁体   中英

Encoding JSON with double quotes instead of single quotes

I have a json.php file which delivers results like this:

{ "markers": [ {'a1_id':"4213CK58", etc.

The problem is that the Google Maps API doesn't like single quotes and so I need my results like this:

{ "markers": [ {"a1_id":"4213CK58", etc.

Replacing ' by " in the code doesn't deliver...

<?php 

    // Iterate over the rows
    $nextRow= $result->nextRow();
    $r      = 1;
    $info   = array();

    while ( $nextRow ) {


        $nextColumn = $result->nextColumn();

        // Has this column been printed already
        if ( $unique ) 
        {
            $d = $result->getDataForField($unique);
            if ( array_key_exists($d, $already) )
            {
                $nextRow= $result->nextRow();
                continue;
            }
            $already[$d] = true;
        }

        echo '{';
        // Iterate over the columns in each row

        while ( $nextColumn )
        {

            // Get the variable
            $variable       = $result->getOutputVariable();
            $name           = $variable->getName(true);
            $data           = $result->getDataForField();

            if ( !isset($info[$name]) ) {
                $info[$name]['translate']   = $variable->shouldTranslate();
                $info[$name]['type']        = $variable->getDataType();
                $info[$name]['linkable']    = $variable->isLinkable();
            }

            // Translate the data if requested
            if ( $info[$name]['translate'] ) {
                $data   = LQMTemplate::_($data);
            }

            $data   = $variable->format($data, false);

            $type   = $info[$name]['type'];
            if ( ($type == 'bool') or ($type == 'boolean') )
            {
                $data = $data ? '1' : '0';
                echo "'$name':$data";
            } elseif ( $encode ) {
                // Can we use json_encode ?
                // str_replace because some versions of PHP have a bug that will over escape forward slashes
                echo "'$name':".str_replace('\\/', '/', json_encode($data));
            } else {
                $data   = LQMUtility::jsonEscape($data, '"');
                echo "'$name':\"$data\"";
            }

            // Conditionally print the next column
            $nextColumn = $result->nextColumn();
            if ( $nextColumn ) echo ",\n ";

        }


        // Conditionally print the next column
        $nextRow = $result->nextRow();

        echo $nextRow ? "},\n" : "}\n";
        $r++;

    } 

unset($result);
echo ']}';
}

}

创建一个包含要编码的数据的数组,然后使用PHP的内置json_encode()函数。

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