为什么json_encode返回一个空字符串 [英] Why would json_encode return an empty string

查看:203
本文介绍了为什么json_encode返回一个空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有3个嵌套数组的简单php结构.

I have a simple php structure with 3 nested arrays.

我不使用特定的对象,而是使用2个嵌套循环构建数组.

I do not use particular objects and I build myself the arrays with 2 nested loops.

这是我要转换为Json的数组的var_dump的示例.

Here is a sample of the var_dump of the array I want to convert to Json.

array (size=2)
  'tram B' => 
    array (size=2)
      0 => 
        array (size=3)
          'name' => string 'Ile Verte' (length=9)
          'distance' => int 298
          'stationID' => int 762
      1 => 
        array (size=3)
          'name' => string 'La Tronche Hôpital' (length=18)
          'distance' => int 425
          'stationID' => int 771
  16 => 
    array (size=4)
      0 => 
        array (size=3)
          'name' => string 'Bastille' (length=8)
          'distance' => int 531
          'stationID' => int 397
      1 => 
        array (size=3)
          'name' => string 'Xavier Jouvin' (length=13)
          'distance' => int 589
          'stationID' => int 438

在另一个脚本中,我具有类似的结构,并且json_encode正常工作. 所以我不明白为什么json_encode在这里不起作用.

In another script I have a similar structure and json_encode works fine. So I don't understand why json_encode won't work here.

编码似乎有问题.当mb_detect_encoding返回ASCII时,json_encode起作用,但是当它返回UTF8时,它不再起作用.

Edit : there seems to be a problem with the encoding. When mb_detect_encoding returns ASCII, the json_encode works but when it returns UTF8, it doesn't work anymore.

Edit2:json_last_error()返回JSON_ERROR_UTF8,这意味着:格式错误的UTF-8字符,可能编码不正确.

Edit2 : json_last_error() returns JSON_ERROR_UTF8 which means : Malformed UTF-8 characters, possibly incorrectly encoded.

推荐答案

挖掘2小时后的效果(参见修改")

Well after 2 hours of digging (cf Edits)

我发现以下内容:

  • 就我而言,这是一个编码问题
  • mb_detect_encoding可能返回错误的响应,某些字符串可能不是UTF-8
  • 在这些字符串上使用
  • utf8_encode()解决了我的问题,但请参阅下面的说明
  • In my case it's a encoding problem
  • mb_detect_encoding returns probably a faulty response, some strings were probably not UTF-8
  • using utf8_encode() on those string solved my problem, but see note below

这是一个递归函数,可以强制将数组中包含的所有字符串转换为UTF-8:

Here is a recursive function that can force convert to UTF-8 all the strings contained in an array:

function utf8ize($d) {
    if (is_array($d)) {
        foreach ($d as $k => $v) {
            $d[$k] = utf8ize($v);
        }
    } else if (is_string ($d)) {
        return utf8_encode($d);
    }
    return $d;
}

像这样简单地使用它:

echo json_encode(utf8ize($data));

注意: utf8_encode()对ISO-8859-根据文档,此字符串为1个UTF-8字符串,因此,如果不确定输入编码 iconv() mb_convert_encoding()如评论和其他解决方案中所述,可能是更好的选择.

Note: utf8_encode() encodes ISO-8859-1 string to UTF-8 as per the docs so if you are unsure of the input encoding iconv() or mb_convert_encoding() may be better options as noted in comments and other solutions.

这篇关于为什么json_encode返回一个空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆