php 生成json,输出了NULL,何解?

WBOY
Release: 2016-06-23 13:13:48
Original
1187 people have browsed it

$a=mysql_query("SELECT * FROM `qq` WHERE userid='$userid'");
$id= 1;
while($row=mysql_fetch_array($a)){
$name = file_get_contents("http://zx.52reso.com/pznc.php?id=2224503435");
$user =new User();
$user->id = $id++;
$user->qq = $row["qq"];
$user->txUrl ='http://q.qlogo.cn/headimg_dl?dst_uin='.$row["qq"].'&spec=100&img_type=jpg';
$user->qqName = $name;
$user->pczx = $row["pczx"];
$user->txgj = $row["txgj"];
$user->iphone = $row["iphone"];
$user->qqmusic = $row["qqmusic"];
$user->xunzhang = $row["xunzhang"];
$user->qqgame = $row["qqgame"];
$user->qqpay = $row["qqpay"];
$user->qqvip = $row["qqvip"];
$user->qqhtdy = $row["qqhtdy"];
$user->qqhtyy = $row["qqhtyy"];
$user->time = $row["time"];
$data[]=$user;

}
$json = json_encode($data);
echo "{".'"msg"'.":true,"."\"nr\":".$json."}";


输出后,qqName是null值,这$name明明是有文本值的,却输出null我就想不明白了,特来求问


回复讨论(解决方案)

json_encode 只用于 utf-8 编码

你的 $user->qqName 是这么来的
$name = file_get_contents("http://zx.52reso.com/pznc.php?id=2224503435");
$user->qqName = $name;

但是
var_dump(mb_check_encoding($name, 'utf-8')); //bool(false)
var_dump(mb_check_encoding($name, 'gbk')); //bool(true)
知道原因了吗?


json_encode 只用于 utf-8 编码

你的 $user->qqName 是这么来的
$name = file_get_contents("http://zx.52reso.com/pznc.php?id=2224503435");
$user->qqName = $name;

但是
var_dump(mb_check_encoding($name, 'utf-8')); //bool(false)
var_dump(mb_check_encoding($name, 'gbk')); //bool(true)
知道原因了吗?




谢谢,解决了,转码一下
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template