dou4121 2011-10-29 10:46
浏览 90
已采纳

json_encode php结果为NULL? [重复]

Possible Duplicate:
json_encode is returning NULL?

I'm having a strange problem with json_encode() in php. Pretty simple code:

$content = json_encode(array('content1' => $arm_length,
                             'content2' => $body_length));
echo $content;

$arm_length and $body_length variables contain the HTML markup for two select dropdown menus. My problem is when it echo's out it show's NULL for content1 and content2. If I take the json_encode() away and just do print_r($content) it shows all the data as it should be.

Does anyone know what's happening here? Is there certain data that can't be parsed into JSON? I've done this a few times now using Ajax/PHP and never had any problems.

Cheers for any help with this.

  • 写回答

2条回答 默认 最新

  • duangai2831 2011-10-29 10:49
    关注

    json_encode() has the (undocumented) habit of silently nulling properties that contain invalid (= non-UTF-8) characters.

    Make sure your input data is UTF-8 encoded, which is a documented requirement of that function.

    In the event of a failure to encode, json_last_error() can be used to determine the exact nature of the error. (Available in PHP 5.3 only)

    Related: How to keep json_encode() from dropping strings with invalid characters

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器