dsfsda121545 2012-06-02 05:41
浏览 110
已采纳

使用PHP的JSON解析返回unicode / ascii字符

I have a code lik this

            <?php

            $jsonurl = "http://api.tamilmagazine.com/";
            $json = file_get_contents($jsonurl,0,null,null);
            $json_output = json_decode($json);
            print_r($json_output);
            ?>

The above code returns a json response as.

        stdClass Object
        (
            [mag_id] => 1
            [mag_name] => ஆனநà¯à®¤ விகடனà¯
            [sid] => 544
            [bpath] => http://www.tamilmagazine.com/
            [api_path] => http://api.tamilmagazine.com/
            [categories] => Array
                (
                    [0] => stdClass Object
                        (
                            [cat_id] => 25
                            [cat_name] => அரசியலà¯
                            [articles] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [aid] => 20053
                                            [a_title] => தலையஙà¯à®•à®®à¯ - கறà¯à®±à¯à®•à¯ கொடà¯à®™à¯à®•à®³à¯ மேதைகளே...
                                            [p_no] => 7
                                            [weburl] => tamilmagazine/msite.php?type=article&mid=1
                                        )



                            . . .. .. . . . . . . . . . . . . . . . . . . . and so on . . . .

When i view the given api (http://api.tamilmagazine.com/) in browser i am getting the correct font lik this.

            {
                "mag_id": "190987",
                "mag_name": "தென்மேற்கு பருவமழை கேரளாவில் வரும் 5ம் துவங்கும் ",
                "sid": "44",
                "bpath": "http://www.tamilmagazine.com/",
                "api_path": "http://api.tamilmagazine.com/",
                "categories": [
                    {
                        "cat_id": "25",
                        "cat_name": "தென்மேற்கு பருவமழை கேரளாவில் வரும் 5ம் துவங்கும் ",
                        "articles": [
                            {
                                "aid": "3",
                                "a_title": "தென்மேற்கு பருவமழை கேரளாவில் வரும் 5ம் துவங்கும் ...",
                                "p_no": "7",
                                "weburl": "msitee.php?type=article&mid=1"
                            },



            .  .. . . . .   and so on. . ... 

In other words, my api url works fine in browser, whereas, the api url after php parsing did not work for me in my browser, showing some special characters (i dont know whether its unicode or ascii).

Please advice.

Thanks Haan

  • 写回答

2条回答 默认 最新

  • drcmg28484 2012-06-02 06:01
    关注

    This is probably correct in the server - but viewing in a broswer it looks wrong. If you don't specify the character encoding of the output, or the setting in php.ini is wrong, then the browser will guess, and often gets it wrong.

    A couple of ways to test:

    1. Check the "source" by doign a view source. (Make sure you can view the source in a compatible text editor, though - otherwise the same might happen)

    2. BEFORE you do the debug output, add in the correct HTML headers first to set the encoding:

    echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body>';   // Set the charset appropriately! Looks like a cyrillic set?
    print_r($json_output);
    echo '</body></html>';
    
    1. Next to check is your PHP output has the correct header. php.ini has a default setting for output: "default_charset". If this is blank, or wrong, set this to the apprpriate value either in php.ini or using ini_set(); and this wil tell php to specify a character encoding header.

    2. You could also specify the encoding header using header() - but the php.ini directive may clash, so use one or the other.

    3. Finally, if that fails, you need to decode manually. Check the comment in the manual (http://php.net/manual/en/function.json-decode.php) by "contacto at hardcode dot com dot 25-Nov-2010 01:53" as this has an example function


    Other tricks you can use when handling the actual string would be to use

    echo utf8_decode(print_r($json_output, true));
    

    as this may convert to ISO more readily understood by the browser IF the original is UTF-8. Probably it's not utf-8 otherwise the browser should have understood and displayed it appropriately, but worth a shot if all else fails.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?