json_encode php结果为空?(复制)

时间:2022-12-21 21:34:10

Possible Duplicate:
json_encode is returning NULL?

可能的重复:json_encode返回的是NULL吗?

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

我对php中的json_encode()有一个奇怪的问题。非常简单的代码:

$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.

$arm_length和$body_length变量包含两个选择下拉菜单的HTML标记。我的问题是,当echo输出时,content1和content2显示为空。如果我去掉json_encode(),只执行print_r($content),它将显示所有数据。

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.

有人知道这里发生了什么吗?是否有某些数据不能解析为JSON?我已经用Ajax/PHP做过几次了,从来没有遇到过任何问题。

Cheers for any help with this.

为这方面的任何帮助干杯。

2 个解决方案

#1


4  

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

json_encode()有一个(未文档说明的)习惯:静默地取消包含无效(=非utf -8)字符的属性。

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

确保输入数据是UTF-8编码的,这是该函数的文档要求。

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)

如果编码失败,可以使用json_last_error()来确定错误的确切性质。(只适用于PHP 5.3)

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

相关:如何防止json_encode()使用无效字符删除字符串

#2


0  

quotes inside your vars " should be escaped like this: \" you can do that by add_slashes($arm_length)

您的vars中的引号“应该这样转义:\”可以通过add_slashes($arm_length)实现

Also json throws errors on \n (new line) and some other characters, you can find full list here - http://json.org/

另外,json在\n(新行)和其他一些字符上抛出错误,您可以在这里找到完整的列表——http://json.org/。

#1


4  

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

json_encode()有一个(未文档说明的)习惯:静默地取消包含无效(=非utf -8)字符的属性。

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

确保输入数据是UTF-8编码的,这是该函数的文档要求。

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)

如果编码失败,可以使用json_last_error()来确定错误的确切性质。(只适用于PHP 5.3)

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

相关:如何防止json_encode()使用无效字符删除字符串

#2


0  

quotes inside your vars " should be escaped like this: \" you can do that by add_slashes($arm_length)

您的vars中的引号“应该这样转义:\”可以通过add_slashes($arm_length)实现

Also json throws errors on \n (new line) and some other characters, you can find full list here - http://json.org/

另外,json在\n(新行)和其他一些字符上抛出错误,您可以在这里找到完整的列表——http://json.org/。