[英]How to convert Json object of html element into actual HTML element that i can render on view using C# or js or jquery
My requirement is, i want to get a json(in the form of html tag) from DB and convert it into html.我的要求是,我想从数据库中获取一个 json(以 html 标记的形式)并将其转换为 html。
{"input":{
"name":"fname",
"type":"text",
"placeholder":"Enter your firstname",
"Id":"fname"
}
}
{"button" :{
"name":"btn",
"class":"save",
"type":"submit",
"Id":"btn1",
"text":"save"
}
}
i want to convert this json into html tags like我想将此 json 转换为 html 标签,例如
<input type="text" id="fname" name="fname" placeholder="Enter your firstname">
<button type="submit" class="save" id="btn1" name="btn">save</button>
how can i convert this using c# or javascript or jquery anything will help me我如何使用 c# 或 javascript 或 jquery 转换它 任何东西都会帮助我
To solve your problem you need to loop throw your json and createElement along the way.要解决您的问题,您需要沿途循环抛出 json 和 createElement。
let json = {
"input": {
"name": "fname",
"type": "text",
"placeholder": "Enter your firstname",
"id": "fname"
},
"button" : {
"name": "btn",
"class": "save",
"type": "submit",
"id": "btn1",
"text": "save"
}
}
Object.keys(json).forEach( (element) => {
let foo = document.createElement(element)
Object.keys(json[element]).forEach( (nestedAttribute) => {
foo.setAttribute(nestedAttribute, json[element][nestedAttribute]);
})
document.body.appendChild(foo)
})
Object.keys(json)
allow you to get the keys of your json Object.keys(json)
允许您获取 json 的密钥
This way the result looks like this:这样,结果如下所示:
Have a nice day !祝你今天过得愉快 !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.