search
HomeBackend DevelopmentPHP Tutorialphp用json_encode后中文显示null

PHP JSON null

有这一个数组:
Array(    [0] => Array        (            [name] => 192.168.1.250            [id] => 3232236026            [items] => Array                (                    [defualt] => Array                        (                            [parentNode] => 3232236026                            [name] => 请选择接口                            [id] => 0                        )                    [0] => Array                        (                            [parentNode] => 3232236026                            [name] => LAN0(内网)                            [id] => em0                        )                    [1] => Array                        (                            [parentNode] => 3232236026                            [name] => LAN1(外网)                            [id] => em1                        )                )        ))


我在解析前先用mb_convert_encoding()改了成了utf-8

我用json_encode解析后,在linux终端用php xxx.php直接执行,能得到正确的结果:
[{"name":"192.168.1.250","id":"3232236026","items":{"defualt":{"parentNode":"3232236026","name":"\u8bf7\u9009\u62e9\u63a5\u53e3","id":"0"},"0":{"parentNode":"3232236026","name":"LAN0(\u5185\u7f51)","id":"em0"},"1":{"parentNode":"3232236026","name":"LAN1(\u5916\u7f51)","id":"em1"}}}]

但是我用火狐的web控制台打开后,得到的结果却是:
[{"name":"192.168.1.250","id":"3232236026","items":{"defualt":{"parentNode":"3232236026","name":"\u8bf7\u9009\u62e9\u63a5\u53e3","id":"0"},"0":{"parentNode":"3232236026","name":null,"id":"em0"},"1":{"parentNode":"3232236026","name":null,"id":"em1"}}}]

上面的“请选择接口”这个字符串,是我在php中直接输入的,而“LAN1(外网)”是我在一个文件里面获取的,他们有什么不一样吗?该如何解决呢?

回复讨论(解决方案)

1、json_encode 只能处理 utf-8 的数据(可利用命名空间重定义)
2、你的php程序是 utf-8 的,所以“请选择接口”能通过编码
3、你的数据文件不是 utf-8 的,所以“LAN1(外网)”等,不能通过编码

解决的办法是:将数据文件以 utf-8 编码保存

回复1L, 

我编辑的环境都是gbk的,所以我直接在php编辑时输入“请选择接口”后,就用mb_convert_encoding()改成utf-8了,

“LAN1(外网)”也是一样的,从另外一个文件获取后,也用mb_convert_encoding()改成utf-8了。

所以我php xxx.php执行这个php文件能通过,得到正确的结果LAN1(\u5916\u7f51),就是在火狐的web控制台那里打开得到的结果是Null,

我百度了一下,有人说那个文件是用c生成的,里面带有\0,我替换了LAN1(外网)可能有的\0后,结果还是一样的。

你的程序里不是用类似这样的语句吗
$s = json_encode( $ar);

你在其后加上
echo base64_encode(serialize( $ar));
贴出结果

czoyODM6Ilt7Im5hbWUiOiIxOTIuMTY4LjEuMjUwIiwiaWQiOiIzMjMyMjM2MDI2IiwiaXRlbXMiOnsiZGVmdWFsdCI6eyJwYXJlbnROb2RlIjoiMzIzMjIzNjAyNiIsIm5hbWUiOiJcdThiZjdcdTkwMDlcdTYyZTlcdTYzYTVcdTUzZTMiLCJpZCI6IjAifSwiMCI6eyJwYXJlbnROb2RlIjoiMzIzMjIzNjAyNiIsIm5hbWUiOiJMQU4wKFx1NTE4NVx1N2Y1MSkiLCJpZCI6ImVtMCJ9LCIxIjp7InBhcmVudE5vZGUiOiIzMjMyMjM2MDI2IiwibmFtZSI6IkxBTjEoXHU1OTE2XHU3ZjUxKSIsImlkIjoiZW0xIn19fV0iOw==

[{"name":"192.168.1.250","id":"3232236026","items":{"defualt":{"parentNode":"3232236026","name":"\u8bf7\u9009\u62e9\u63a5\u53e3","id":"0"},"0":{"parentNode":"3232236026","name":"LAN0(\u5185\u7f51)","id":"em0"},"1":{"parentNode":"3232236026","name":"LAN1(\u5916\u7f51)","id":"em1"}}}]

上面的是echo base64_encode(serialize($ar));
下面的是直接打印的结果。

让你贴出有问题的,你却贴出正常的


[root@localhost php]# php select_for_interface.php 
PHP Warning:  Directive 'register_globals' is deprecated in PHP 5.3 and greater in Unknown on line 0
czoyODM6Ilt7Im5hbWUiOiIxOTIuMTY4LjEuMjUwIiwiaWQiOiIzMjMyMjM2MDI2IiwiaXRlbXMiOnsiZGVmdWFsdCI6eyJwYXJlbnROb2RlIjoiMzIzMjIzNjAyNiIsIm5hbWUiOiJcdThiZjdcdTkwMDlcdTYyZTlcdTYzYTVcdTUzZTMiLCJpZCI6IjAifSwiMCI6eyJwYXJlbnROb2RlIjoiMzIzMjIzNjAyNiIsIm5hbWUiOiJMQU4wKFx1NTE4NVx1N2Y1MSkiLCJpZCI6ImVtMCJ9LCIxIjp7InBhcmVudE5vZGUiOiIzMjMyMjM2MDI2IiwibmFtZSI6IkxBTjEoXHU1OTE2XHU3ZjUxKSIsImlkIjoiZW0xIn19fV0iOw==
[{"name":"192.168.1.250","id":"3232236026","items":{"defualt":{"parentNode":"3232236026","name":"\u8bf7\u9009\u62e9\u63a5\u53e3","id":"0"},"0":{"parentNode":"3232236026","name":"LAN0(\u5185\u7f51)","id":"em0"},"1":{"parentNode":"3232236026","name":"LAN1(\u5916\u7f51)","id":"em1"}}}]
[root@localhost php]#

上面是我的操作结果, 这个结果貌似是正常的,但是用浏览器打开后却是不正常的:
czoyNTE6Ilt7Im5hbWUiOiIxOTIuMTY4LjEuMjUwIiwiaWQiOiIzMjMyMjM2MDI2IiwiaXRlbXMiOnsiZGVmdWFsdCI6eyJwYXJlbnROb2RlIjoiMzIzMjIzNjAyNiIsIm5hbWUiOiJcdThiZjdcdTkwMDlcdTYyZTlcdTYzYTVcdTUzZTMiLCJpZCI6IjAifSwiMCI6eyJwYXJlbnROb2RlIjoiMzIzMjIzNjAyNiIsIm5hbWUiOm51bGwsImlkIjoiZW0wIn0sIjEiOnsicGFyZW50Tm9kZSI6IjMyMzIyMzYwMjYiLCJuYW1lIjpudWxsLCJpZCI6ImVtMSJ9fX1dIjs=
[{"name":"192.168.1.250","id":"3232236026","items":{"defualt":{"parentNode":"3232236026","name":"\u8bf7\u9009\u62e9\u63a5\u53e3","id":"0"},"0":{"parentNode":"3232236026","name":null,"id":"em0"},"1":{"parentNode":"3232236026","name":null,"id":"em1"}}}]

这就是浏览器中记录的结果。

我要你给出的是 json_encode 之前的数据,好像我 #3 的描述不会产生歧义吧?




YToxOntpOjA7YTozOntzOjQ6Im5hbWUiO3M6MTM6IjE5Mi4xNjguMS4yNTAiO3M6MjoiaWQiO3M6MTA6IjMyMzIyMzYwMjYiO3M6NToiaXRlbXMiO2E6Mzp7czo3OiJkZWZ1YWx0IjthOjM6e3M6MTA6InBhcmVudE5vZGUiO3M6MTA6IjMyMzIyMzYwMjYiO3M6NDoibmFtZSI7czoxNToi6K+36YCJ5oup5o6l5Y+jIjtzOjI6ImlkIjtzOjE6IjAiO31pOjA7YTozOntzOjEwOiJwYXJlbnROb2RlIjtzOjEwOiIzMjMyMjM2MDI2IjtzOjQ6Im5hbWUiO3M6MTI6IkxBTjAo5YaF572RKSI7czoyOiJpZCI7czozOiJlbTAiO31pOjE7YTozOntzOjEwOiJwYXJlbnROb2RlIjtzOjEwOiIzMjMyMjM2MDI2IjtzOjQ6Im5hbWUiO3M6MTI6IkxBTjEo5aSW572RKSI7czoyOiJpZCI7czozOiJlbTEiO319fX0=

没看仔细。。。

URLencode一下在json吧然后在decode回来了不知行不行

[{"name":"192.168.1.250","id":"3232236026","items":{"defualt":{"parentNode":"3232236026","name":"\u8bf7\u9009\u62e9\u63a5\u53e3","id":"0"},"0":{"parentNode":"3232236026","name":"LAN0(\u5185\u7f51)","id":"em0"},"1":{"parentNode":"3232236026","name":"LAN1(\u5916\u7f51)","id":"em1"}}}]
又是给了正确的数据




YToxOntpOjA7YTozOntzOjQ6Im5hbWUiO3M6MTM6IjE5Mi4xNjguMS4yNTAiO3M6MjoiaWQiO3M6MTA6IjMyMzIyMzYwMjYiO3M6NToiaXRlbXMiO2E6Mzp7czo3OiJkZWZ1YWx0IjthOjM6e3M6MTA6InBhcmVudE5vZGUiO3M6MTA6IjMyMzIyMzYwMjYiO3M6NDoibmFtZSI7czoxNToi6K+36YCJ5oup5o6l5Y+jIjtzOjI6ImlkIjtzOjE6IjAiO31pOjA7YTozOntzOjEwOiJwYXJlbnROb2RlIjtzOjEwOiIzMjMyMjM2MDI2IjtzOjQ6Im5hbWUiO3M6MTI6IkxBTjAo5YaF572RKSI7czoyOiJpZCI7czozOiJlbTAiO31pOjE7YTozOntzOjEwOiJwYXJlbnROb2RlIjtzOjEwOiIzMjMyMjM2MDI2IjtzOjQ6Im5hbWUiO3M6MTI6IkxBTjEo5aSW572RKSI7czoyOiJpZCI7czozOiJlbTEiO319fX0=

没看仔细。。。

回复10L,这个确实是json_encode()之前的数组的数据了。

另外,我测试了一下,就算从文件里面读取的全是英文,火狐web控制台也是得到的Null,
同时,我将内容写死在php代码里面,火狐web控制台得到的就是正确结果。

结论:貌似只要是从文件里面读取的,经过json_encode()后,火狐web控制台得到的就是null。

但是我在其他机器上也使用过从文件读取内容,通过json_encode()后在界面展示,没出现过这样的问题啊。

浏览器得到的只是 json_encode 执行后的结果,并且也不会去改变接受到的数据
所以如果是 null 那么一定是在 json_encode 时产生的

我要你提供的正是你发现有 null 时的原始数据,这样才能找到问题出现的原因
抗燃,如果你不愿意,我更无所谓

浏览器得到的只是 json_encode 执行后的结果,并且也不会去改变接受到的数据
所以如果是 null 那么一定是在 json_encode 时产生的

我要你提供的正是你发现有 null 时的原始数据,这样才能找到问题出现的原因
抗燃,如果你不愿意,我更无所谓
老大,这个地址的问题:http://bbs.csdn.net/topics/390507272
就算不解答,也烦请告诉下原因啊。

浏览器得到的只是 json_encode 执行后的结果,并且也不会去改变接受到的数据
所以如果是 null 那么一定是在 json_encode 时产生的

我要你提供的正是你发现有 null 时的原始数据,这样才能找到问题出现的原因
抗燃,如果你不愿意,我更无所谓

我8L发的那一段确实是我json_encode()之前的那个数组, 用print_r打印出来的就是我主楼贴的那个数组。
现象是终端打印结果是正常的, 火狐控制台看就异常了, 截图如下:

从图中可以看出来, php xxx.php执行后的结果,也就是echo出来的结果中带LAN1(\u5916\u7f51),


而在火狐控制台,火狐收到的结果却是null。

然后,我接下来做测试, 将读取文件的内容,不通过读取,直接写死到php代码中,


从这个截图可以看出,echo出来的内容,和上一楼基本是一样的


但是在火狐这里得到结果,却天差地别。这个结果是对的。

同时, 我读取文件里面去掉中文,全英文,得到的结果也是Null,感觉读取文件,火狐的显示Null,不读取就正常

不是有
=> 323236026 [name] => [id] => em0
这样的吗?
name 至少是不可见字符,那么怎么能正确转换呢?

你这是 ajax 应用吧?你可以还是火狐操作
但把 echo base64_encode(serialize($ar)); 改成
file_put_contents('test.txt', base64_encode(serialize($ar)));
然后贴出 test.txt 的内容

是的,ajax调用php,php通过echo返回一个json对象给ajax进行后续处理。

test.txt里面的内容:
YToxOntpOjA7YTozOntzOjQ6Im5hbWUiO3M6MTM6IjE5Mi4xNjguMS4yNTAiO3M6MjoiaWQiO3M6MTA6IjMyMzIyMzYwMjYiO3M6NToiaXRlbXMiO2E6Mzp7czo3OiJkZWZ1YWx0IjthOjM6e3M6MTA6InBhcmVudE5vZGUiO3M6MTA6IjMyMzIyMzYwMjYiO3M6NDoibmFtZSI7czoxNToi6K+36YCJ5oup5o6l5Y+jIjtzOjI6ImlkIjtzOjE6IjAiO31pOjA7YTozOntzOjEwOiJwYXJlbnROb2RlIjtzOjEwOiIzMjMyMjM2MDI2IjtzOjQ6Im5hbWUiO3M6MTI6IkxBTjAo5YaF572RKSI7czoyOiJpZCI7czozOiJlbTAiO31pOjE7YTozOntzOjEwOiJwYXJlbnROb2RlIjtzOjEwOiIzMjMyMjM2MDI2IjtzOjQ6Im5hbWUiO3M6MTI6IkxBTjEo5aSW572RKSI7czoyOiJpZCI7czozOiJlbTEiO319fX0=

这是火狐得到的内容:
YToxOntpOjA7YTozOntzOjQ6Im5hbWUiO3M6MTM6IjE5Mi4xNjguMS4yNTAiO3M6MjoiaWQiO3M6MTA6IjMyMzIyMzYwMjYiO3M6NToiaXRlbXMiO2E6Mzp7czo3OiJkZWZ1YWx0IjthOjM6e3M6MTA6InBhcmVudE5vZGUiO3M6MTA6IjMyMzIyMzYwMjYiO3M6NDoibmFtZSI7czoxNToi6K+36YCJ5oup5o6l5Y+jIjtzOjI6ImlkIjtzOjE6IjAiO31pOjA7YTozOntzOjEwOiJwYXJlbnROb2RlIjtzOjEwOiIzMjMyMjM2MDI2IjtzOjQ6Im5hbWUiO047czoyOiJpZCI7czozOiJlbTAiO31pOjE7YTozOntzOjEwOiJwYXJlbnROb2RlIjtzOjEwOiIzMjMyMjM2MDI2IjtzOjQ6Im5hbWUiO047czoyOiJpZCI7czozOiJlbTEiO319fX0=

file_put_contents('test.txt', base64_encode(serialize($arr_select)));
$json = json_encode($arr_select);
print_r($arr_select);
echo base64_encode(serialize($arr_select));
echo "\n";
echo $json;

我这样应该是打印出的版主要的内容了吧。

你说一下,你 #17 的两段数据是怎么来的?
都是经火狐请求而保存到文件里的吗?
如果是,那么为什么不一样?

你说一下,你 #17 的两段数据是怎么来的?
都是经火狐请求而保存到文件里的吗?
如果是,那么为什么不一样?

那两段数据, test.txt里面就是执行版主你的语句得到的, 同时也是echo base64_encode(serialize($arr_select));的结果。

而第二段数据,是火狐web控制台跟踪http请求,有火狐打印出来的响应的内容。

就是不知道为什么会不一样, 因为不管怎么改, php里面在Linux终端直接echo得到的确实是有LAN1(\u5916\u7f51),而火狐出现的确实Null, 这种情况我也是第一次遇到, 读文件,不管有没有中文,火狐打印的都是null, 

我换一台机器部署一下我的代码试试看, 看看是不是环境的原因。

我要你把
file_put_contents('test.txt', base64_encode(serialize($arr_select)));
放在你的程序里,然后用火狐按正常流程访问你的程序
这样得到的 test.txt,才是真实的数据

我要你把
file_put_contents('test.txt', base64_encode(serialize($arr_select)));
放在你的程序里,然后用火狐按正常流程访问你的程序
这样得到的 test.txt,才是真实的数据


不好意思啊,版主, 我换个环境试了下, 倒是没发现问题,后来看了你21楼的提示,发现是文件权限的原因,我直接在Linux终端直接执行,用的root权限,能够正常打印,但是在浏览器打印的时候,用的apche权限,权限不足,所以没有打开那个文件

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What are some common problems that can cause PHP sessions to fail?What are some common problems that can cause PHP sessions to fail?Apr 25, 2025 am 12:16 AM

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

How do you debug session-related issues in PHP?How do you debug session-related issues in PHP?Apr 25, 2025 am 12:12 AM

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

What happens if session_start() is called multiple times?What happens if session_start() is called multiple times?Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

How do you configure the session lifetime in PHP?How do you configure the session lifetime in PHP?Apr 25, 2025 am 12:05 AM

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.

What are the advantages of using a database to store sessions?What are the advantages of using a database to store sessions?Apr 24, 2025 am 12:16 AM

The main advantages of using database storage sessions include persistence, scalability, and security. 1. Persistence: Even if the server restarts, the session data can remain unchanged. 2. Scalability: Applicable to distributed systems, ensuring that session data is synchronized between multiple servers. 3. Security: The database provides encrypted storage to protect sensitive information.

How do you implement custom session handling in PHP?How do you implement custom session handling in PHP?Apr 24, 2025 am 12:16 AM

Implementing custom session processing in PHP can be done by implementing the SessionHandlerInterface interface. The specific steps include: 1) Creating a class that implements SessionHandlerInterface, such as CustomSessionHandler; 2) Rewriting methods in the interface (such as open, close, read, write, destroy, gc) to define the life cycle and storage method of session data; 3) Register a custom session processor in a PHP script and start the session. This allows data to be stored in media such as MySQL and Redis to improve performance, security and scalability.

What is a session ID?What is a session ID?Apr 24, 2025 am 12:13 AM

SessionID is a mechanism used in web applications to track user session status. 1. It is a randomly generated string used to maintain user's identity information during multiple interactions between the user and the server. 2. The server generates and sends it to the client through cookies or URL parameters to help identify and associate these requests in multiple requests of the user. 3. Generation usually uses random algorithms to ensure uniqueness and unpredictability. 4. In actual development, in-memory databases such as Redis can be used to store session data to improve performance and security.

How do you handle sessions in a stateless environment (e.g., API)?How do you handle sessions in a stateless environment (e.g., API)?Apr 24, 2025 am 12:12 AM

Managing sessions in stateless environments such as APIs can be achieved by using JWT or cookies. 1. JWT is suitable for statelessness and scalability, but it is large in size when it comes to big data. 2.Cookies are more traditional and easy to implement, but they need to be configured with caution to ensure security.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version