在网上看到json的文章,打算测试一下,结果,出现错误,先附上代码:
<?php
@header("Content-type:text/html;charset=GB2312");
$arr = array ('name'=>"贾朝藤",'age'=>19,'sex'=>"男",'add'=>"中国石家庄");
echo json_encode($arr);
echo "<br/>";
$name = iconv("GBK","UTF-8","贾朝藤");
$sex = iconv("GBK","UTF-8","男");
$add = iconv("GBK","UTF-8","中国石家庄");
$arr = array ('name'=>"$name",'age'=>19,'sex'=>"$sex",'add'=>"$add");
echo $str=json_encode($arr);
?>
错误信息如下:{"name":"\u8d3e\u671d\u85e4","age":19,"sex":"\u7537","add":"\u4e2d\u56fd\u77f3\u5bb6\u5e84"}Notice: iconv() [function.iconv]: Detected an illegal character in input string in D:\wamp\www\ajax\js_php.php on line 6Notice: iconv() [function.iconv]: Detected an illegal character in input string in D:\wamp\www\ajax\js_php.php on line 7Notice: iconv() [function.iconv]: Detected an illegal character in input string in D:\wamp\www\ajax\js_php.php on line 8
{"name":"\u7490\u70ac","age":19,"sex":"","add":"\u6d93"} 之后百度,谷歌了一番,得到两个方法:
1.把gb2312换为gbk,同样出错,错误信息如下
{"name":"\u8d3e\u671d\u85e4","age":19,"sex":"\u7537","add":"\u4e2d\u56fd\u77f3\u5bb6\u5e84"}Notice: iconv() [function.iconv]: Detected an incomplete multibyte character in input string in D:\wamp\www\ajax\js_php.php on line 6Notice: iconv() [function.iconv]: Detected an incomplete multibyte character in input string in D:\wamp\www\ajax\js_php.php on line 7Notice: iconv() [function.iconv]: Detected an illegal character in input string in D:\wamp\www\ajax\js_php.php on line 8
{"name":"\u7490\u70ac\u6e5e\u9498","age":19,"sex":"\u9422","add":"\u6d93"} 2.在UTF-8后加//IGNORE,加上后提示如下:
{"name":"\u8d3e\u671d\u85e4","age":19,"sex":"\u7537","add":"\u4e2d\u56fd\u77f3\u5bb6\u5e84"}Notice: iconv() [function.iconv]: Detected an incomplete multibyte character in input string in D:\wamp\www\ajax\js_php.php on line 6Notice: iconv() [function.iconv]: Detected an incomplete multibyte character in input string in D:\wamp\www\ajax\js_php.php on line 7
{"name":"\u7490\u70ac\u6e5e\u9498","age":19,"sex":"\u9422","add":"\u6d93\u9365\u754c\u7176\u7039\u8dfa\u7c1e"} 希望大家帮忙解决一下问题,谢谢!
<?php
@header("Content-type:text/html;charset=GB2312");
$arr = array ('name'=>"贾朝藤",'age'=>19,'sex'=>"男",'add'=>"中国石家庄");
echo json_encode($arr);
echo "<br/>";
$name = iconv("GBK","UTF-8","贾朝藤");
$sex = iconv("GBK","UTF-8","男");
$add = iconv("GBK","UTF-8","中国石家庄");
$arr = array ('name'=>"$name",'age'=>19,'sex'=>"$sex",'add'=>"$add");
echo $str=json_encode($arr);
?>
错误信息如下:{"name":"\u8d3e\u671d\u85e4","age":19,"sex":"\u7537","add":"\u4e2d\u56fd\u77f3\u5bb6\u5e84"}Notice: iconv() [function.iconv]: Detected an illegal character in input string in D:\wamp\www\ajax\js_php.php on line 6Notice: iconv() [function.iconv]: Detected an illegal character in input string in D:\wamp\www\ajax\js_php.php on line 7Notice: iconv() [function.iconv]: Detected an illegal character in input string in D:\wamp\www\ajax\js_php.php on line 8
{"name":"\u7490\u70ac","age":19,"sex":"","add":"\u6d93"} 之后百度,谷歌了一番,得到两个方法:
1.把gb2312换为gbk,同样出错,错误信息如下
{"name":"\u8d3e\u671d\u85e4","age":19,"sex":"\u7537","add":"\u4e2d\u56fd\u77f3\u5bb6\u5e84"}Notice: iconv() [function.iconv]: Detected an incomplete multibyte character in input string in D:\wamp\www\ajax\js_php.php on line 6Notice: iconv() [function.iconv]: Detected an incomplete multibyte character in input string in D:\wamp\www\ajax\js_php.php on line 7Notice: iconv() [function.iconv]: Detected an illegal character in input string in D:\wamp\www\ajax\js_php.php on line 8
{"name":"\u7490\u70ac\u6e5e\u9498","age":19,"sex":"\u9422","add":"\u6d93"} 2.在UTF-8后加//IGNORE,加上后提示如下:
{"name":"\u8d3e\u671d\u85e4","age":19,"sex":"\u7537","add":"\u4e2d\u56fd\u77f3\u5bb6\u5e84"}Notice: iconv() [function.iconv]: Detected an incomplete multibyte character in input string in D:\wamp\www\ajax\js_php.php on line 6Notice: iconv() [function.iconv]: Detected an incomplete multibyte character in input string in D:\wamp\www\ajax\js_php.php on line 7
{"name":"\u7490\u70ac\u6e5e\u9498","age":19,"sex":"\u9422","add":"\u6d93\u9365\u754c\u7176\u7039\u8dfa\u7c1e"} 希望大家帮忙解决一下问题,谢谢!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货