search
Homephp教程php手册json_decode返回null空问题解决方法

json_decode对于英文估计返回都是正常的但有些朋友碰到中文时会就发现返回为null或空了,下面我来给各位朋友简单介绍此问题解决方法.

今天在做一个东西的时候需要抓取淘宝的一些数据,找到了请求的url,返回了一个callback,看了下callback中的参数是一个对象,通过正则匹配去到了数组,但是在使用json_decode()转换的时候返回的是NULL,老郁闷了,前一段时间要做一个东西也是因为这个原因,没有成功的把json对象转换成php的数组,放弃了,今天又遇到了,终于找到了解决的办法.

原因在于:抓取的数数据是是GBK格式,通过抓包看到,返回的header头中,代码如下:

Content-Type:text/html;charset=GBK

这个时候用icvo转码下,然后在json_decode()就可以正常转换了,代码如下:

iconv('gbk','utf-8',$data[1][0]);

这里还需要主要的是你php代码文件的格式,建议是utf-8无bom头.


本文地址:

转载随意,但请附上文章地址:-)

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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

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

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version