<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <button onclick="myFunction()">点我</button> <p id="demo">"</p> <script> function myFunction() { // var str = document.getElementById("demo").innerHTML; var str = [{"userid":"running","username":"LISI","depart_id":1,"depart_name":"微WEI","gender":1,"email":""}] strr = JSON.stringify(str) //转化json字符串 var dd= "\"" var txt = strr.replace(/"/g,dd) // 对于String对象的replace方法,表达式不加入g,则只替换第一个匹配,如果加入g,则替换所有匹配 document.getElementById("demo").innerHTML = txt; } </script> </body> </html>
JSON.stringify(str) //转化json字符串
strr.replace(/"/g,dd) //正则匹配替换,
对于String对象的replace方法,表达式不加入g,则只替换第一个匹配,如果加入g,则替换所有匹配