php中json_decode()的使用方法

2022-11-29 16:11:41 浏览数 (1)

后台json_decode()序列化编码示例:

代码语言:javascript复制
<?php
$id=$_GET["id"];
$userName=$_GET["userName"];
$arrayName = array('id' => $id,'userName'=>$userName);
#把数组序列化
echo json_encode($arrayName);
?>

json输出并解析:

代码语言:javascript复制
<script type="text/javascript" src="https://code.jquery.com/jquery-3.4.0.min.js"></script>
<script>
    $(function() {
        $.ajax({
            url: "index.php",
            data: "id=666&userName=admin",
            type: "get",
            gettype: "json",
            success: function(result) {
                console.log(result);
                var re = eval("("   result   ")")
                console.log(re["id"]);
                console.log(re["userName"]);
            },
        });
    });
</script>

0 人点赞