dongqiuqiu4736 2013-02-07 03:35
浏览 62

JSON字符编码

I have some European Charactacter in mySQL database table. My JSON code returns null object for those. I have also defined the header and utf8 encode still the result is null. I have attached my code snippets here

Header:

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

JSON encoding

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 results in the firebug:

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

Here in the JSON result one field is null in the name of University since the name of the university is in European characters.

  • 写回答

1条回答 默认 最新

  • duanrongshi1544 2013-02-07 05:14
    关注

    Finally I get my answer fixed by changing this

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

    into

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

    I encode the the whole out put returned into utf-8 and now the characters can be seen in the datatables. Hope it helps somebody.

    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等