1.JSON.stringify():js object->json
<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 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!