0

我在 mySQL 数据库表中有一些欧洲字符。我的 JSON 代码为这些返回 null 对象。我还定义了标头和 utf8 编码,结果仍然为空。我在这里附上了我的代码片段

标题:

header("Content-Type: text/javascript; charset=utf-8");

JSON编码

while ( $aRow = mysql_fetch_array( $rResult ) )
{
    $row = array();
    for ( $i=0 ; $i<count($aColumns) ; $i++ )
    {
        if ( $aColumns[$i] == "version" )
        {
            /* Special output formatting for 'version' column */
            $row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
        }
        else if ( $aColumns[$i] != ' ' )
        {
            /* General output */
            $row[] = $aRow[ $aColumns[$i] ];
        }
    }
    $output['aaData'][] = $row;
}

echo utf8_encode(json_encode( $output ));

JSON导致萤火虫:

{"sEcho":1,"iTotalRecords":"190458","iTotalDisplayRecords":"4","aaData":[["University of Toronto ","Canada","72","17","41","144","7","16","6"],["The University of British Columbia ","Canada","71","22","44","176","11","16","7"],["University of Calgary","Canada","65","31","45","145","11","15","6"],[null,"Canada","63","18","45","125","12","26","6"]]}

在 JSON 结果中,大学名称中的一个字段为空,因为大学名称是欧洲字符。

4

1 回答 1

0

最后我通过改变这个得到了我的答案

        $row[] = $aRow[ $aColumns[$i] ];

进入

    $row[] = utf8_encode($aRow[ $aColumns[$i] ]);

我将返回的整个输出编码为 utf-8,现在可以在数据表中看到这些字符。希望它可以帮助某人。

于 2013-02-07T05:14:47.573 回答