json_encode详解,转义
1.json_encod基本用法:数组转字符串
<?php $arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5); echo json_encode($arr); ?> 以上例程会输出: {"a":1,"b":2,"c":3,"d":4,"e":5}
2.json_decode基本用法:字符串转数组
<?php $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; var_dump(json_decode($json)); ?> 输出 array{ ["a"] => int(1) ["b"] => int(2) ["c"] => int(3) ["d"] => int(4) ["e"] => int(5)
3.json_encode在输出汉字的时候会被转义
$arr4 = array('t1'=>'测试下中文汉字','t2'=>'www.jd.com','t3'=>'123456789','t4'=>"!@#$%^&*",'t5'=>'汉字+www.jd.com+465454'); print_r(json_encode($arr4));exit;
输出结果:(汉字都会被转义)
{"t1":"\u6d4b\u8bd5\u4e0b\u4e2d\u6587\u6c49\u5b57","t2":"www.jd.com","t3":"123456789","t4":"!@#$%^&*","t5":"\u6c49\u5b57+www.jd.com+465454"}
4.如何保证json_encode不被转义
$arr5 = array('t1'=>urlencode('测试下中文汉字')); // 重点1 print_r(urldecode(json_encode($arr5))); // 重点2
输出结果:
{"t1":"测试下中文汉字"}
5.urlencode,json_encode,urldecode使用
$arr6 = urlencode('测试下中文汉字'); print_r($arr6);
print_r(json_encode($arr6));
print_r(urldecode($arr6));
print_r(urldecode(json_encode($arr6)));
输出结果:
%E6%B5%8B%E8%AF%95%E4%B8%8B%E4%B8%AD%E6%96%87%E6%B1%89%E5%AD%97 // $arr6 输出
"%E6%B5%8B%E8%AF%95%E4%B8%8B%E4%B8%AD%E6%96%87%E6%B1%89%E5%AD%97" // json_encode输出
测试下中文汉字 // urldecode输出结果
"测试下中文汉字" // urldecode+json_encode输出结果
5.1 JSON_UNESCAPED_UNICODE (非常好用)
json_encode("中华人民共和国",JSON_UNESCAPED_UNICODE) // php 版本必须大于5.4
输出结果:"中华人民共和国"
6.Jquery调取php的json_encode内容
php代码
$arr = Array ( [goods_id] => 1 [goods_sn] => LX000000 [goods_name] => 保税区直发 德国Aptamil爱他美婴幼儿配方奶粉Pre段800g 【2罐起发】 ) echo json_encode($arr)
输出格式
{"id":"1","user_id":"1","goods_id":"1","goods_stock":"6","goods_sn":"LX000000","shop_price":168}
js输出json格式内容
function t(n,v){ res = Ajax.call('user.php?act=depot_id', 'id=1',null,"GET", "JSON",false); var goods_title = "#goods_title_"+n; var goods_name = "#goods_name_"+n; var goods_price = "#goods_price_"+n; $(goods_title).val(res.goods_title); $(goods_name).val(res.goods_name); $(goods_price).val(res.goods_price); alert(res.goods_name); //document.getElementById(div).value = '11'; }
7.json_decode 有stdClass Object,解决办法json_decode($s,true),
$s = '[{"goods_id":"2","goods_num":2,"goods_name":"u8377u5170Nutrilonu725bu680fu5976u7c89 u30102u7f50u8d77u53d1u3011"},{"goods_id":"2","goods_num":2,"goods_name":"u8377u5170Nutrilonu725bu680fu5976u7c89 u30102u7f50u8d77u53d1u3011"}]';
$f = json_decode($s);
输出:
Array ( [0] => stdClass Object ( [goods_id] => 2 [goods_num] => 2 [goods_name] => u8377u5170Nutrilonu725bu680fu5976u7c89 u30102u7f50u8d77u53d1u3011 ) [1] => stdClass Object ( [goods_id] => 2 [goods_num] => 2 [goods_name] => u8377u5170Nutrilonu725bu680fu5976u7c89 u30102u7f50u8d77u53d1u3011 ) )
解决办法:
$f = json_decode($s,true);
print_r($f);
输出:
Array ( [0] => Array ( [goods_id] => 2 [goods_num] => 2 [goods_name] => u8377u5170Nutrilonu725bu680fu5976u7c89 u30102u7f50u8d77u53d1u3011 ) [1] => Array ( [goods_id] => 2 [goods_num] => 2 [goods_name] => u8377u5170Nutrilonu725bu680fu5976u7c89 u30102u7f50u8d77u53d1u3011 ) )
8.json汉字被转义 JSON_UNESCAPED_UNICODE 最简单方法
return json_encode($arr, JSON_UNESCAPED_UNICODE); // php 5.3以上版本可以使用 (JSON_UNESCAPED_UNICODE)