为什么JSON字符串为空?

时间:2022-09-25 14:43:13

I have written a code in PHP , to see the JSON string output. But i am getting null value.

我用PHP编写了一个代码,以查看JSON字符串的输出。但我得到的是空值。

<?php

 $l=array();

 $l[] = array('a'=>'@cÐaÐjÔÐ J kf _ÞÒi^ ','b'=>']éÞ[ѯРQtÍ]hà_ , `ËSÐ J heZ Òhi');

 echo $j = json_encode($l);

?>

Output-:

输出-:

  [{"a":null,"b":null}] 

Why the JSON output is coming in null. I am expecting that there must be character encoding problem.

为什么JSON输出是null ?我希望一定有字符编码的问题。

i want the output in following format.

我希望输出的格式如下。

[{"a":"@cÐaÐjÔÐ J kf _ÞÒi^","b":"]éÞ[ѯРQtÍ]hà_ , `ËSÐ J heZ Òhi"}] 

Please help me out. Please suggest me some solution

请帮帮我。请给我一些解决办法

Thanks in Advance !!!

提前谢谢! ! !

2 个解决方案

#1


1  

You may need to use utf8_encode() the string before pushing to array and then json_encode since json_encode() works only only with utf8 encoded data

您可能需要在推入数组之前使用utf8_encode()字符串,然后使用json_encode(),因为json_encode()只使用utf8编码的数据

$l=array();

$l[] = array('a'=>utf8_encode('@cÐaÐjÔÐ J kf _ÞÒi^ '),
'b'=>utf8_encode(']éÞ[ѯРQtÍ]hà_ , `ËSÐ J heZ Òhi'));

echo json_encode($l);

Ok looks like your issue is not so simple,and you need to use

看来你的问题不是那么简单,你需要使用

htmlentities( (string) $value, ENT_QUOTES, 'utf-8', FALSE); 

to handle the situation

处理情况

$array = array("a"=>htmlentities( (string) "@cÐaÐjÔÐ J kf _ÞÒi^ ", ENT_QUOTES, 'utf-8', FALSE),
               "b"=>htmlentities( (string) "]éÞ[ѯРQtÍ]hà_ , `ËSÐ J heZ Òhi", ENT_QUOTES, 'utf-8', FALSE)

             );

$json = json_encode($array);
echo ($json);

check here http://phpfiddle.org/main/code/mh8-7ua

检查这里http://phpfiddle.org/main/code/mh8-7ua

You need to add as above in your array elements.

您需要在数组元素中添加如上所述。

#2


0  

I've recreated your described behaviour using a limited encoding.

我使用有限的编码重新创建了您所描述的行为。

Check the file encoding of your php script. Make sure that it is set to unicode or utf8 if it is available.

检查php脚本的文件编码。如果可用,请确保将其设置为unicode或utf8。

Otherwise you'll have to convert your string to utf8 first like Abhik posted in his answer.

否则,你必须先把你的字符串转换成utf8,就像阿布希克在他的回答中所写的那样。

Your output will then look like

您的输出将会是这样的

[{"a":"@c\u00d0a\u00d0j\u00d4\u00d0 J kf _\u00de\u00d2i^ ",
  "b":"]\u00e9\u00de[\u00d1\u00af\u00d0 Qt\u00cd]h\u00e0_ , `\u00cbS\u00d0 J heZ \u00d2hi"}]

That is working as intended, since...

因为……

Any character may be escaped. If the character is in the Basic
Multilingual Plane (U+0000 through U+FFFF), then it may be
represented as a six-character sequence: a reverse solidus, followed
by the lowercase letter u, followed by four hexadecimal digits that
encode the character's code point. The hexadecimal letters A though
F can be upper or lowercase. So, for example, a string containing
only a single reverse solidus character may be represented as
"\u005C".

任何角色都可以被转义。如果字符在基本的多语言平面中(U+0000通过U+FFFF),那么它可以表示为一个6字符序列:一个反向的孤立点,后面跟着小写的字母U,后面跟着四个十六进制数字,编码字符的代码点。十六进制字母A, F可以是大写或小写。因此,例如,一个只包含一个反向solidus字符的字符串可以表示为“\u005C”。

#1


1  

You may need to use utf8_encode() the string before pushing to array and then json_encode since json_encode() works only only with utf8 encoded data

您可能需要在推入数组之前使用utf8_encode()字符串,然后使用json_encode(),因为json_encode()只使用utf8编码的数据

$l=array();

$l[] = array('a'=>utf8_encode('@cÐaÐjÔÐ J kf _ÞÒi^ '),
'b'=>utf8_encode(']éÞ[ѯРQtÍ]hà_ , `ËSÐ J heZ Òhi'));

echo json_encode($l);

Ok looks like your issue is not so simple,and you need to use

看来你的问题不是那么简单,你需要使用

htmlentities( (string) $value, ENT_QUOTES, 'utf-8', FALSE); 

to handle the situation

处理情况

$array = array("a"=>htmlentities( (string) "@cÐaÐjÔÐ J kf _ÞÒi^ ", ENT_QUOTES, 'utf-8', FALSE),
               "b"=>htmlentities( (string) "]éÞ[ѯРQtÍ]hà_ , `ËSÐ J heZ Òhi", ENT_QUOTES, 'utf-8', FALSE)

             );

$json = json_encode($array);
echo ($json);

check here http://phpfiddle.org/main/code/mh8-7ua

检查这里http://phpfiddle.org/main/code/mh8-7ua

You need to add as above in your array elements.

您需要在数组元素中添加如上所述。

#2


0  

I've recreated your described behaviour using a limited encoding.

我使用有限的编码重新创建了您所描述的行为。

Check the file encoding of your php script. Make sure that it is set to unicode or utf8 if it is available.

检查php脚本的文件编码。如果可用,请确保将其设置为unicode或utf8。

Otherwise you'll have to convert your string to utf8 first like Abhik posted in his answer.

否则,你必须先把你的字符串转换成utf8,就像阿布希克在他的回答中所写的那样。

Your output will then look like

您的输出将会是这样的

[{"a":"@c\u00d0a\u00d0j\u00d4\u00d0 J kf _\u00de\u00d2i^ ",
  "b":"]\u00e9\u00de[\u00d1\u00af\u00d0 Qt\u00cd]h\u00e0_ , `\u00cbS\u00d0 J heZ \u00d2hi"}]

That is working as intended, since...

因为……

Any character may be escaped. If the character is in the Basic
Multilingual Plane (U+0000 through U+FFFF), then it may be
represented as a six-character sequence: a reverse solidus, followed
by the lowercase letter u, followed by four hexadecimal digits that
encode the character's code point. The hexadecimal letters A though
F can be upper or lowercase. So, for example, a string containing
only a single reverse solidus character may be represented as
"\u005C".

任何角色都可以被转义。如果字符在基本的多语言平面中(U+0000通过U+FFFF),那么它可以表示为一个6字符序列:一个反向的孤立点,后面跟着小写的字母U,后面跟着四个十六进制数字,编码字符的代码点。十六进制字母A, F可以是大写或小写。因此,例如,一个只包含一个反向solidus字符的字符串可以表示为“\u005C”。