Javascript中JS对象与JSON的相互转换


本文摘自PHP中文网,作者逆旅行人,侵删。

2021041214304520807.jpg

1.JSON.stringify():js对象->json

1

2

3

4

5

6

7

8

9

10

11

12

13

<script>

       //Number

      console.log(JSON.stringify(3.14));

      //字符串

      console.log(JSON.stringify('php.cn'));

      //布尔型

      console.log(JSON.stringify(true));

      console.log(JSON.stringify(null));

      //对象

      console.log(JSON.stringify({x:'a',y:'b'}));

      //数组

      console.log(JSON.stringify([1,2,3]));

</script>

2.JSON.parse(): json->js对象

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<script>

    console.log(JSON.parse(

        `{

            "a":1,

            "b":2,

            "c":3

        }`

    ));

    console.log(typeof JSON.parse(

        `{

            "a":1,

            "b":2,

            "c":3

        }`));

    let jsObj=JSON.parse(`{"a":1,"b":2,"c":3}`);

    //判断是否为Object

    console.log(jsObj instanceof Object);

    console.log(typeof jsObj);

    //promise fetch

</script>

推荐:《2021年js面试题及答案(大汇总)》

以上就是Javascript中JS对象与JSON的相互转换的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

javascript中js对象与json的相互转换

更多相关阅读请进入《JS对象、JSON、Javascript》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...