- 將PHP對(duì)象轉(zhuǎn)為Json數(shù)據(jù)
$arr = array(1, 2, 'Hello', 'Json', array('name' => '國哥', 'sex' => '男'));
$jsonArr = json_encode($arr);
將Json數(shù)據(jù)解析為PHP對(duì)象
$arr = json_decode($jsonArr);-
當(dāng)數(shù)據(jù)存在中文轉(zhuǎn)為Json格式的時(shí)候中文轉(zhuǎn)為unicode值的兩種解決方案
- 在保存數(shù)據(jù)時(shí)將中文數(shù)據(jù)使用urlencode()進(jìn)行編碼,轉(zhuǎn)為Json數(shù)據(jù)時(shí)用urldecode()
$arr = array(1, 2, 'Hello', 'Json', array('name' => urlencode('國哥'), 'sex' => urlencode('男')));
$jsonArr = urldecode(json_encode($arr));2. 在轉(zhuǎn)換為Json數(shù)據(jù)的時(shí)候傳入第二個(gè)參數(shù):JSON_UNESCAPED_UNICODE(適用于PHP5.4及以上)$arr = array(1, 2, 'Hello', 'Json', array('name' => '國哥', 'sex' => '男'));
$jsonArr =json_encode($arr,JSON_UNESCAPED_UNICODE); - 在保存數(shù)據(jù)時(shí)將中文數(shù)據(jù)使用urlencode()進(jìn)行編碼,轉(zhuǎn)為Json數(shù)據(jù)時(shí)用urldecode()
相關(guān)資料
json_encode
json_decode
urlencode
urldecode