jQuery-工具方法-parseJSON方法

二次信任 提交于 2019-11-29 06:38:18
<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script src="./jquery.js"></script>
    <script>

        var str1 = '{"firstName": "莱昂纳多","lastName": "小沈阳","age": 20,"gender": "male"}';
        var str2 = '["red", "green", "blue"]';
        //  使用 $.parseJSON() 方法解析 JSON 格式的字符串
        var obj = $.parseJSON(str1)
        var colors = $.parseJSON(str2)

        console.log(obj.age)
        console.log(colors[0])

        // 使用原生的 JSON.parse() 方法也能实现相同的效果


    </script>
</body>

</html>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!