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

查看:29
本文介绍了为什么 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 小时的挖掘(cf Edits)

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天全站免登陆