Javascript 如何将选定的 HTML 转换为 Json?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34504050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert selected HTML to Json?
提问by yan
I want to save part of my html code into json as a file then recap back the html codes for editing. Any idea how can i do it?
我想将我的 html 代码的一部分作为文件保存到 json 中,然后回顾 html 代码以进行编辑。知道我该怎么做吗?
<div id='TextBoxesGroup'>
<div id="TextBoxDiv1">
<label class="draggable ui-widget-content clickableLabel" id="label1" >New Text</label>
<input id='textbox1' class="clickedit" type="text" class="draggable" class="ui-widget-content" placeholder="Text Here"/>
<div class="clearfix"></div>
</div>
</div>
I am new to json, please simplified if possible.I had look at other questions but their don't seem to address my question
我是 json 新手,如果可能,请简化。我看过其他问题,但他们似乎没有解决我的问题
回答by lleaff
What you want to do is called serializing.
您想要做的称为序列化。
// This gives you an HTMLElement object
var element = document.getElementById('TextBoxesGroup');
// This gives you a string representing that element and its content
var html = element.outerHTML;
// This gives you a JSON object that you can send with jQuery.ajax's `data`
// option, you can rename the property to whatever you want.
var data = { html: html };
// This gives you a string in JSON syntax of the object above that you can
// send with XMLHttpRequest.
var json = JSON.stringify(data);
回答by Nilesh Chavan
function htmlToJson(div,obj){
if(!obj){obj=[]}
var tag = {}
tag['tagName']=div.tagName
tag['children'] = []
for(var i = 0; i< div.children.length;i++){
tag['children'].push(htmlToJson(div.children[i]))
}
for(var i = 0; i< div.attributes.length;i++){
var attr= div.attributes[i]
tag['@'+attr.name] = attr.value
}
return tag
}
回答by Shijin TR
var html = $('#TextBoxesGroup')[0].outerHTML;
var temp = {"html":html};
var obj = JSON.parse(temp);
console.log(obj); // shows json object
You can use any server side language to make a json from obj.
您可以使用任何服务器端语言从 obj 生成 json。
回答by john Anstaett
see this link on w3school https://www.w3schools.com/code/tryit.asp?filename=FR0BHTAPG78A
在 w3school 上查看此链接 https://www.w3schools.com/code/tryit.asp?filename=FR0BHTAPG78A
mytext = document.getElementById("xx").innerHTML;
var myObj = {innerHTML:"yyy"};
myObj.innerHTML = mytext;
myJSON = JSON.stringify(myObj);
回答by ahmad eskandarzadeh
i use recursive function to handle it
我使用递归函数来处理它
from bs4 import BeautifulSoup
dic = dict()
itt = 0
def list_tree_names(node):
global itt
for child in node.contents:
try:
dic.update({child.name +"/"+ str(itt): child.attrs})
itt += 1
list_tree_names(node=child)
except:
dic.update({"text" +"/"+ str(itt): child})
itt += 1
soup = BeautifulSoup(data, "html.parser")
data is the html text
数据是html文本
list_tree_names(soup)
print(dic)
you can see json file in https://github.com/celerometis/html2json
你可以在https://github.com/celerometis/html2json 中看到 json 文件
回答by Jatin Patel
You can use this following snippet to convert HTML into JSON string
您可以使用以下代码段将 HTML 转换为 JSON 字符串
var HtmlToJsonString = JSON.stringify($("#TextBoxesGroup").html());
You can stored this JSON string into database and edit time you decode it and put on UI page.
您可以将此 JSON 字符串存储到数据库中,并编辑您解码它并放在 UI 页面上的时间。