1.$json只能utf-8編碼
//$json = mb_convert_encoding($json, 'utf8', 'gbk');
//$json = iconv('gbk', 'utf-8//ignore', $json);
2.元素最後不能有逗號(與php的array不同)
3.元素不能使用單引號
$json = str_replace("'", '"', $json);
4.元素值中間不能有空格和n,必須替換
$json = str_replace(array("/r/n", "/r", "/n","/t"), "", $json);
5. 去掉bom頭
試了兩種方式能去除掉:
1
2
3
$result
= trim(
$result
,
"\xef\xbb\xbf"
);
print_r(json_decode(
$result
, true));
exit
;
還有一種比較矬:
1
2
3
4
5
$result
= @iconv(
"utf-8"
,
"gbk//ignore"
,
$result
);
$result
= @iconv(
"gbk"
,
"utf-8//ignore"
,
$result
);
print_r(json_decode(
$result
, true));
exit
;
ajax處理返回的json格式資料
以使用者註冊為例 register.php regisgerprocess.php 這裡兩句話很重要,第一講話告訴瀏覽器返回的資料格式,若返回xml格式資料,此處寫header content type text xmla set utf 8 若返回tex或json資料,此處填寫header con...
json返回資料,屬性值為null或空的問題
當使用com.alibaba.fastjson.support.spring.fastjsonhttpmessageconverter轉換返回資料為json時,會省略掉為空為null的屬性值。解決辦法在sping mvc.xml配置json轉換器時加上對空和null的處理,配置如下 json轉換器 ...
json資料的返回處理和編碼轉換
其實,這倆個是可以分開不同2個問題,json資料的返回,編碼轉換,不過,我遇到的問題,需要把他們整合一下。問題 ajax請求回去的,是亂碼,白班getbytes 之後無奈,就連tomcat的server.xml配置都改了,加了uliencode utf 8 於是,想著用json資料返回看看。首先,在...