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条)

报告相同问题?