JavaScript object into a string, the string into an object

  1. Object is converted to a string

    JSON.stringify()
    
  2. String into an object

    JSON.parse()
    
  3. For example:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <canvas id="canvas"></canvas>
    <script>
        let a = [1,2,3];
        let a1 = JSON.stringify(a);
        let a2 = JSON.parse(a1);
        let b = {'name':'a','age':18};
        let b1 = JSON.stringify(b);
        let b2 = JSON.parse(b1);
        console.log(a1);
        console.log(a2);
        console.log(b1);
        console.log(b2);
    </script>
    </body>
    </html>
    

    Here Insert Picture Description

Published 270 original articles · won praise 123 · views 10000 +

Guess you like

Origin blog.csdn.net/KaiSarH/article/details/104696212