Javascript 我想在JSON.stringify()之后播放htmlDecode

Javascript 我想在JSON.stringify()之后播放htmlDecode,javascript,html,json,dom,Javascript,Html,Json,Dom,我想将值中带引号的字符串解析为JSON 在JSON.stringify()的字符串为HtmlCode()之后;转换为“并且当JSON.parse()时发生错误 在此过程中新建DOMParser().parseFromString(输入,“text/html”) 除“QUOTE”外,是否可以执行该命令;" 还是有别的办法 <script> const str = "&lt ;h3&gt ;&amp ;&amp ;&amp ;&

我想将值中带引号的字符串解析为JSON

在JSON.stringify()的字符串为HtmlCode()之后;转换为“并且当JSON.parse()时发生错误

在此过程中新建DOMParser().parseFromString(输入,“text/html”) 除“QUOTE”外,是否可以执行该命令;"

还是有别的办法

  <script>
    const str = "&lt ;h3&gt ;&amp ;&amp ;&amp ;&quot ;&quot ;xx;;&lt ;/h3&gt ; &lt ;h2&gt ;";
    const obj = { "test1": "&lt ;h3&gt ;&amp ;&amp ;&amp ;&quot ;&quot ;xx;;&l t;/h3&gt ; &lt ;h2&gt ; ", "test2": "help" };

    function htmlDecode(input) {
      var doc = new DOMParser().parseFromString(input, "text/html");
      return doc.documentElement.textContent;
    }

    console.log(htmlDecode(str))    // <h3>&&&""xx;;</h3> <h2>
    console.log(htmlDecode(JSON.stringify(obj)))  // {"test1":"<h3>&&&""xx;;</h3> <h2> ","test2":"help"}
    console.log(JSON.parse(htmlDecode(JSON.stringify(obj)))) // VM49:1 Uncaught SyntaxError: Unexpected string in JSON at position 18 at JSON.parse (<anonymous>)
  </script>
</body>


const str=“<;h3>&&&&引用&引用;xx</h3>&书信电报;h2>;";
const obj={“test1”:“h3>;<;h3>;<;h2>;”,“test2”:“help”};
函数htmlDecode(输入){
var doc=new DOMParser().parseFromString(输入,“text/html”);
返回doc.documentElement.textContent;
}
console.log(htmlDecode(str))/&&&&“xx;”;;
log(htmlDecode(JSON.stringify(obj))/{“test1”:“&&&&”xx;;”,“test2”:“help”}
console.log(JSON.parse(htmlDecode(JSON.stringify(obj)))/VM49:1未捕获的语法错误:JSON.parse()位置18处的JSON中出现意外字符串
如果字符串不包括qout,那么JSON解析就很好了

const quotIsNotObj = { "test1": "&lt ;h3&gt ;&amp ;&amp ;&amp ;xx;;&lt ;/h3&gt ; &lt ;h2&gt ; ", "test2": "help" };

console.log(JSON.parse(htmlDecode(JSON.stringify(successObj)))) // {"test1":"<h3>&&&xx;;</h3> <h2> ","test2":"help"} 
const quotIsNotObj={“test1”:“h3>;<;/h3>;<;h2>;,“test2”:“help”};
console.log(JSON.parse(htmlDecode(JSON.stringify(successObj)))/{“test1”:“&&&&xx;;”,“test2”:“help”}

您可以转义对象中包含的字符串。这将解析为JSON

const str=“h3&&&;""xx ;/h3 h2”;
const obj={'test1':'h3&;”“xx;;/h3 h2','test2':'help'};
console.log(htmlDecode(str))/&&&&“xx;”;;
log(htmlDecode(JSON.stringify(obj))/{“test1”:“&&&&”xx;;”,“test2”:“help”}
//使用转义字符串创建新对象
var newObj={};
Object.keys(obj).forEach(函数(key){
var newVal=转义限制(htmlDecode(obj[key]);
newObj[key]=newVal;
});
console.log(newObj);
函数htmlDecode(输入){
var doc=new DOMParser().parseFromString(输入,“text/html”);
返回doc.documentElement.textContent;
}
//转义引号之类的字符
功能转义限制(jsonStr){
返回jsonStr.replace(/\\n/g,“\\n”)
.replace(/\\'/g,“\\'”)
.replace(/\\“/g,\\\”)
.替换(/\&/g,“\\&”)
.替换(/\\r/g,“\\r”)
.替换(/\\t/g,“\\t”)
.替换(/\\b/g,“\\b”)
.替换(/\\f/g,“\\f”);

}
JSON与HTML无关-您所做的一切毫无意义,您无法将HTML“解析”为JSON@JaromandaX我想一次解码json中的所有字符串。如果值不是qout,则解析得很好。哦,对了,所以
textContent
不是正确的json-抱歉,我没有阅读
htmlDecode
中的完整代码(假设你返回了html,我的错)-是的……这是一个问题。1)你的JSON中的空格是一个格式错误吗?按照这里的显示方式,它们不会得到那样的输出。2)你想以JS对象或html结束吗?谢谢。除了其他所有内容。是否可以使用一个.replace(/\\“/g,\\”)问题        查看和应用答案的示例:                       函数htmlDecode(输入){                                     var doc=new DOMParser().parseFromString(输入,“text/html”);                            doc=doc.replace(/“/g,\\\”)                          return doc.documentElement.textContent;}该方法不合适。函数decodeThMLINJSON(json){return json.parse(new DOMParser().parseFromString(json.stringify(json).replace(/“/g,,\ \”),“text/html”).documentElement.textContent);}                                                 为什么不适合在项目中使用此选项?这对于quote来说是静态的。如果字符串将包含其他特殊字符呢?在更改了(new DOMParser().parseFromString(input,“text/html”))中的所有特殊字符后,这不就是qout吗;在执行JSON.parse()时会出现问题吗?