代码之家  ›  专栏  ›  技术社区  ›  Lizard

php json_encode返回null

  •  8
  • Lizard  · 技术社区  · 14 年前
    Array
    (
        [sEcho] => 1
        [iTotalRecords] => 7521
        [iTotalDisplayRecords] => 1
        [aaData] => Array
            (
                [0] => Array
                    (
                        [0] => Nordic Capital Buys SiC Processing
                        [1] => 2010-06-21/nordic-capital-buys-sic-processing
                        [2] => PEHub Media
                        [3] => Business
                        [4] => completed
                        [5] => Nordic Capital has acquired a 70% stake in SiC Processing AG, a German industrial recycling company, from Frog Capital. No sale price was disclosed.  SiC Processing’s founding family retains a 25% holding, while former lead investor Zouk Ventures retains a 5% stake.
    
                        [6] => Admin, China, Frog Capital, Germany, Italy, Iyad Omari, Manufacturing, Norway, PEHub Media, Photovoltaic Wafer Manufacturing, Renewable Energy, Semiconductor, United States
                    )
    
            )
    
    )
    
    echo json_encode($myArr);
    

    {“sEcho”:“1”,“iTotalRecords”:7521,“iTotalDisplayRecords”:“1”,“aaData”:[[” 北欧资本收购碳化硅加工公司</a></div>“,” 无效的 ," Admin,中国,Frog Capital,德国,意大利,Iyad Omari,制造业,挪威,PEHub Media,光伏晶圆制造,可再生能源,半导体,美国]]}

    请注意完成后字符串中间的null

    为什么会这样,我需要执行什么转义/操作才能对其进行编码?

    addslashes

    5 回复  |  直到 12 年前
        1
  •  24
  •   robjmills    14 年前

    根据手册:

    包含非utf值的数组, 生成的JSON字符串。你可以 成批编码 使用数组映射函数的数组:

    $encodedArray = array_map(utf8_encode, $myArr);
    echo json_encode($encodedArray);
    
        2
  •  1
  •   Luca Matteis    14 年前
        3
  •  1
  •   AllenJB    14 年前

    在5.2.13为我工作。确保您至少使用了PHP5.2.0,并且PHP编译时没有使用--disable json。您可能还需要检查是否启用了错误报告(和/或日志记录)。

        4
  •  0
  •   franklin joseph    9 年前

    更简单的方法是 $store_name = utf8_encode(name_of_varaible) 但请确保你的角色集是 ISO-8859-1.