[英]How can I render an html from a json-like string?
You can use JSON.parse to parse your data and remove all the unwanted slashes.您可以使用JSON.parse来解析您的数据并删除所有不需要的斜杠。 Your response is in string format at current.
您的回复目前是字符串格式。
Also you access the object you need to use .
您还可以访问需要使用的 object
.
you get the value of that key.你得到那个键的值。 In your case its
Vocabulary
在你的情况下,它的
Vocabulary
Vocabulary
We can use textContent()Vocabulary
我们可以使用textContent()element.style.fontSize
element.style.fontSize
element.style.color
element.style.color
Run snippet below.运行下面的代码段。
//Your response in string format let response = '{\"attributes\":{\"size\":\"18px\",\"color\":\"#3ba0dc\"},\"insert\":\"Vocabulary:\"}' //Parse data let parsedData = JSON.parse(response) //Show text on div using querySelector let element = document.querySelector('#data') //Apply response data to your Vocabulary: element.textContent = parsedData.insert; element.style.fontSize = parsedData.attributes.size; element.style.color = parsedData.attributes.color; //Console log console.log(parsedData)
<div id="data"></div>
This is stringify format of JSON
data.这是
JSON
数据的字符串化格式。 so you need to parse it.所以你需要解析它。
console.log( JSON.parse( '{\"attributes\":{\"size\":\"18px\",\"color\":\"#3ba0dc\"},\"insert\":\"Vocabulary:\"}' ) );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.