如何让json_encode()与ISO-8859-1(åäö)一起使用

Joh*_*han 11 php json iso-8859-1

json_encode()当我使用åäö时,不会为我工作.为什么?我怎样才能让它发挥作用?

php:

echo json_encode($arr);
Run Code Online (Sandbox Code Playgroud)

javascript:

var theResponse = JSON.parse(xmlHttp.responseText);
Run Code Online (Sandbox Code Playgroud)

当我alert()回复时,响应中包含å,ä或ö,响应为=NULL

拜托,帮帮我......

Gre*_*reg 12

它在json_encode()文档中说:

此功能仅适用于UTF-8编码数据.

您应该首先使用iconvmbstring将其转换为utf-8 .


Joh*_*han 7

正如格雷格所说,我不得不对åäö进行编码UTF-8.但我没有使用iconv或mbstring.当我utf8_encode()把所有值都放到array问题上之前就解决了问题.


Pau*_*ips 6

此函数将为JSON输出转换正确的数据类型,并对字符串进行utf8_encode.

    /* Change data-type from string to integar or float if required.
     * If string detected then utf8_encode() it. */
    function cast_data_types ($value) {
      if (is_array($value)) {
        $value = array_map('cast_data_types',$value);
        return $value;
      }
      if (is_numeric($value)) {
        if(strpos('.', $value)===false) return (float)$value;
        return (int) $value;
      }
      return utf8_encode((string)$value);
    }

json_encode (cast_data_types($data));
Run Code Online (Sandbox Code Playgroud)