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 Stata链式中介效应代码修改
  • ¥15 latex投稿显示click download
  • ¥15 请问读取环境变量文件失败是什么原因?
  • ¥15 在若依框架下实现人脸识别
  • ¥15 添加组件无法加载页面,某块加载卡住
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用