php 如何让 json_encode() 与 ISO-8859-1 (???) 一起工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1394305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:23:22  来源:igfitidea点击:

How to get json_encode() to work with ISO-8859-1 (???)

phpjsoniso-8859-1

提问by Johan

json_encode()wont work for me when I'm using ???. Why? And how can I get it to work?

json_encode()当我使用时对我不起作用???。为什么?我怎样才能让它工作?

The php:

php

echo json_encode($arr);

The javascript:

javascript

var theResponse = JSON.parse(xmlHttp.responseText);

When I alert()the response, and the response contains ?, ? or ?, the response is = NULL

当我alert()响应时,响应中包含 ?, ? 或 ?,响应为 =NULL

Please, help me out...

请帮帮我...

采纳答案by Johan

As Greg mentioned, I had to encode ??? to UTF-8. But I did't use iconv or mbstring. When I utf8_encode()all values before putting the values to the arraythe problem was solved.

正如格雷格提到的,我必须编码???到UTF-8。但我没有使用 iconv 或 mbstring。当我将utf8_encode()所有值放入值之前array,问题就解决了。

回答by Greg

It says in the json_encode()documentation:

它在json_encode()文档中说:

This function only works with UTF-8 encoded data.

此函数仅适用于 UTF-8 编码数据。

You should convert it to utf-8 with iconvor mbstringfirst.

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

回答by Paul Phillips

This function will cast the correct data type for the JSON output and utf8_encode the strings.

此函数将为 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));

回答by Bernd Ott

JSON defines strings as Unicode!

JSON 将字符串定义为 Unicode!

JSON Definition

JSON 定义

You have to encode you ISO to UTF-8

您必须将 ISO 编码为 UTF-8

回答by Michael Butler

Old question, but figured I'd put this here in case someone needs to log data using json_encode but keep the data untouched, intact for inspection later.

老问题,但我想我会把它放在这里,以防有人需要使用 json_encode 记录数据但保持数据不变,以备日后检查。

You can encode the data raw using base64_encode, then it will work with json_encode. Later after running json_decode, you can decode the string with base64_decode, you'll get the original data unmodified.

您可以使用 对原始数据进行编码base64_encode,然后它将与json_encode. 稍后运行json_decode,您可以使用 解码字符串base64_decode,您将获得未修改的原始数据。

回答by George

As of PHP 5.4.0:

从 PHP 5.4.0 开始:

Convert your strings in your array to into utf-8using utf8_encode($str)function.

将数组中的字符串转换为utf-8usingutf8_encode($str)函数。

Then json_encodewith JSON_UNESCAPED_UNICODEoption:

然后json_encode使用JSON_UNESCAPED_UNICODE选项:

$arr = json_encode($array, JSON_UNESCAPED_UNICODE);

$arr = json_encode($array, JSON_UNESCAPED_UNICODE);

回答by Fabio Montefuscolo

The $data(in my case) is an array with text values as ISO-8859-1. The trick below prepares $datato be used with json_encode.

$data(在我的情况)是文本值数组作为ISO-8859-1。下面的技巧准备$datajson_encode.

function toUtf8(&$v, $k) {
    $v = utf8_encode($v);
}
array_walk_recursive($data, 'toUtf8');

回答by BennyBechDk

Using the standard method when reading from MySQL:

从 MySQL 读取时使用标准方法:

$resultArray = array();
while($obj = MySQL_fetch_object($res)) {
 $resultArray[] = $obj;
}
$result = json_encode($resultArray);

The encoding can be done using the following:

可以使用以下方法进行编码:

$resultArray = array();
while($obj = MySQL_fetch_object($res)) {
 foreach($obj as $key => $value) {
  if (!is_null($value)) {
   $obj->$key = utf8_encode($value);
  }
 }
 $resultArray[] = $obj;
}
$result = json_encode($resultArray);

The if is_nullhas to be included so that null fields (e.g., DateTime fields) remain null in the output.

if is_null必须被包括在内,以便空字段(例如,日期时间字段)保持空在输出中。