搞懂php中json_decode()和json_encode()的使用方法

json_encode()
作用:对变量进行 JSON 编码

语法: string json_encode ( mixed $value [, int $options = 0 [, int $depth = 512 ]] )

value :除了resource 类型之外,可以为任何数据类型。所有字符串数据的编码必须是 UTF-8。

options:由以下常量组成的二进制掩码

:JSON_HEX_QUOT,JSON_HEX_TAG,JSON_HEX_AMP,JSON_HEX_APOS,JSON_NUMERIC_CHECK,JSON_PRETTY_PRINT,JSON_FORCE_OBJECT,JSON_PRESERVE_ZERO_FRACTION,JSON_UNESCAPED_UNICODE(避免中文这些转码等,对应数字256),JSON_UNESCAPED_SLASHES(不转义反斜杠,对应的数字64),JSON_PARTIAL_OUTPUT_ON_ERROR。
关于 JSON 常量详情参考JSON 常量页面,也可以参考http://blog.csdn.net/any2mobile/article/details/8717180。

depth:设置最大深度。 必须大于0。

返回值说明:成功则返回 JSON 编码的 string 或者在失败时返回 FALSE 。

如果value是一个数组,如果是无下标的数组(即默认下标0,1,2,3),则返回值是[]包起来的数组,否则,是{}包起来的对象


$arr  = array(1,2,3,4);
$arr2 = array('a','b','c','d');
$arr3 = array(0=>1,1=>2,2=>3,3=>4);
$arr4 = array(1=>1,2=>2,3=>3,4=>4);
$arr5 = array('0'=>1,'1'=>2,'2'=>3,'3'=>4);
$arr6 = array('a'=>'a','b'=>'b','c'=>'c','d'=>'d');
$arr7 = array(
    array(1,2,3,4),
    array('a','b','c','d'),
    array('a'=>'a','b'=>'b','c'=>'c','d'=>'d')
);
$arr8 = array(
    'one' => array(1,2,3,4),
    'two'=> array('a','b','c','d'),
    'three' => array('a'=>'a','b'=>'b','c'=>'c','d'=>'d')
);
var_dump(json_encode($arr));
//结果: [1,2,3,4]
var_dump(json_encode($arr2));
//结果: ["a","b","c","d"]
var_dump(json_encode($arr3));
//结果: [1,2,3,4]
var_dump(json_encode($arr4));
//结果: {"1":1,"2":2,"3":3,"4":4}
var_dump(json_encode($arr5));
//结果: [1,2,3,4]
var_dump(json_encode($arr6));
//结果: {"a":"a","b":"b","c":"c","d":"d"}
var_dump(json_encode($arr7));
//结果: [[1,2,3,4],["a","b","c","d"],{"a":"a","b":"b","c":"c","d":"d"}]
var_dump(json_encode($arr8));
//结果: {"one":[1,2,3,4],"two":["a","b","c","d"],"three":{"a":"a","b":"b","c":"c","d":"d"}}

1.json_encod基本用法:数组转字符串


$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基本用法:字符串转数组

 
$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输出结果

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=jsondecode(f=jsondecode(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=jsondecode(f=jsondecode(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)

你可能感兴趣的:(搞懂php中json_decode()和json_encode()的使用方法)