dpp78272 2012-05-15 07:28
浏览 71
已采纳

使用双引号而不是单引号编码JSON

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 ",
 ";

        }


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

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

    } 

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

}

  • 写回答

1条回答 默认 最新

  • dragon0023 2012-05-15 07:29
    关注

    Create an array containing the data you want to encode and then use PHP's builtin json_encode() function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题