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','t3'=>'123456789','t4'=>"!@#$%^&*",'t5'=>'汉字+www.jd+465454');
print_r(json_encode($arr4));exit;
输出结果:(汉字都会被转义)
{"t1":"\u6d4b\u8bd5\u4e0b\u4e2d\u6587\u6c49\u5b57","t2":"www.jd","t3":"123456789","t4":"!@#$%^&*","t5":"\u6c49
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输出结果
6.Jquery调取php的json_encode内容
$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).ds_title);
$(goods_name).ds_name);
$(goods_price).ds_price);
ds_name);
//ElementById(div).value = '11';
}
phpjson格式化输出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)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。