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

Conversion between JS objects and JSON in Javascript

autoload
Release: 2021-04-18 13:14:33
Original
1413 people have browsed it

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>
Copy after login

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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template