Home >Web Front-end >JS Tutorial >Conversion between JS objects and JSON in Javascript

Conversion between JS objects and JSON in Javascript

autoload
autoloadOriginal
2021-04-12 14:42:201480browse

Conversion between JS objects and JSON in Javascript

1.JSON.stringify():js object->json

  <script>
         //Number
        console.log(JSON.stringify(3.14));
        //字符串
        console.log(JSON.stringify(&#39;php.cn&#39;));
        //布尔型
        console.log(JSON.stringify(true));
        console.log(JSON.stringify(null));
        //对象
        console.log(JSON.stringify({x:&#39;a&#39;,y:&#39;b&#39;}));
        //数组
        console.log(JSON.stringify([1,2,3]));
  </script>

2.JSON.parse(): json->js object

    <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>

Recommended: "2021 js interview questions and answers (large summary )

The above is the detailed content of Conversion between JS objects and JSON in Javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn