Is it possible to convert a HTML table to json like this:
是否可以将一个HTML表转换成这样的json:
Name Gender Age
姓名性别年龄
John M 18
约翰M 18
Mike M 19
麦克米19
Json:
Json:
{"Values" : {
"1" : {
"name": "John",
"Gender": "M",
"Age": "18"
},
"2": {
"name": "Mike",
"Gender": "M",
"Age": "19"
},
}}
I already have this code:
我已经有了这个代码:
(function() {
var jsonArr = [];
var obj = {};
var thNum = document.getElementsByTagName('th').length;
var arrLength = document.getElementsByTagName('td').length;
for(i = 0; i<arrLength;i++){
if(i%thNum==0){
obj = {};
}
var head = document.getElementsByTagName('th')[i%thNum].innerHTML;
var content = document.getElementsByTagName('td')[i].innerHTML;
obj[head] = content;
if(i%thNum==0){
jsonArr.push(obj);
}
}
document.write("<br>"+JSON.stringify(jsonArr));
})();
The script convert this:
脚本转换:
<!DOCTYPE html>
<TABLE border="3" rules="all" bgcolor="#E7E7E7" cellpadding="1" cellspacing="1">
<TR>
<TH align=center><font size="3" face="Arial">Date</font></TH>
<TH align=center><font size="3" face="Arial"><B>Teacher</B></font></TH>
<TH align=center><font size="3" face="Arial">?</font></TH>
<TH align=center><font size="3" face="Arial">Hour</font></TH>
<TH align=center><font size="3" face="Arial">Subject</font></TH>
<TH align=center><font size="3" face="Arial">Class</font></TH>
<TH align=center><font size="3" face="Arial">Room</font></TH>
<TH align=center><font size="3" face="Arial">(Teacher)</font></TH>
<TH align=center><font size="3" face="Arial">(Room)</font></TH>
<TH align=center><font size="3" face="Arial">XYY</font></TH>
<TH align=center><font size="3" face="Arial"><B>Information</B></font></TH>
<TH align=center><font size="3" face="Arial">(Le.) nach</font></TH>
</TR>
<TR><TD align=center><font size="3" face="Arial">24.9.</font></TD>
<TD align=center><font size="3" face="Arial"><B><strike>Dohe</strike></B></font></TD>
<TD align=center><font size="3" face="Arial">Free</font></TD>
<TD align=center><font size="3" face="Arial">1</font></TD>
<TD align=center><font size="3" face="Arial"><strike>Math</strike></font> </TD>
<TD align=center><font size="3" face="Arial">(9)</font> </TD>
<TD align=center><font size="3" face="Arial">---</font> </TD>
<TD align=center><font size="3" face="Arial"><strike>Dohe</strike></font></TD>
<TD align=center><font size="3" face="Arial">A001</font></TD>
<TD align=center> </TD>
<TD align=center> </TD>
<TD align=center><font size="3" face="Arial">Free.</font></TD>
</TR>
</TABLE>
<script>
(function() {
var jsonArr = [];
var obj = {};
var thNum = document.getElementsByTagName('th').length;
var arrLength = document.getElementsByTagName('td').length;
for(i = 0; i < arrLength; i++){
if(i%thNum === 0){
obj = {};
}
var head = document.getElementsByTagName('th')[i%thNum].innerHTML;
var content = document.getElementsByTagName('td')[i].innerHTML;
obj[head] = content;
if(i%thNum === 0){
jsonArr.push(obj);
}
}
document.write("<br>"+JSON.stringify(jsonArr));
})();
</script>
</body>
</html>
to this:
:
[{"Date":"24.9.","Teacher":"Dohe","?":"Free","Hour":"1","Subject":"Math ","Class":"(9) ","Room":"--- ","(Teacher)":"Dohe","(Room)":"A001","XYY":" ","Information":" ","(Le.) nach":"Free."}]
But I need the "Values" tag before and the "1", "2", wich should be created automatically by row. And at least ist it possible to write this into a new .json file like data.json?
但是我需要之前的“值”标签和“1”、“2”标签,它们应该按行自动创建。至少可以把它写到新的。json文件中,比如data。json?
1 个解决方案
#1
1
If you want to represent the array of rows as an object, change jsonArr.push(obj)
to jsonObj[++rowIx] = obj
, where jsonObj
has previously been initialized to {}
, and rowIx
is a counter that has been initialized to 0 (probably simpler than doing math on i
).
如果您希望将行数组表示为对象,请将jsonArr.push(obj)更改为jsonObj[+ rowIx] = obj,其中jsonObj之前被初始化为{},rowIx是一个被初始化为0的计数器(可能比在i上做数学题要简单)。
The expected output you posted is not valid JSON, so I'm assuming that it's supposed to be wrapped by another set of braces:
您发布的预期输出不是有效的JSON,因此我假设它应该由另一组大括号包装:
document.write("<br>"+JSON.stringify({"Values": jsonObj}));
As for saving to a file, this is tricky to do in a portable manner. There are several other answers that address this, including the following:
至于保存到文件中,这在可移植的方式中是很棘手的。关于这个问题,还有其他几个答案,包括:
- Javascript: Create and save file
- Javascript:创建和保存文件
- How to make a browser display a "save as dialog" so the user can save the content of a string to a file on his system?
- 如何让浏览器显示“保存为对话框”,以便用户可以将字符串的内容保存到系统中的文件中?
#1
1
If you want to represent the array of rows as an object, change jsonArr.push(obj)
to jsonObj[++rowIx] = obj
, where jsonObj
has previously been initialized to {}
, and rowIx
is a counter that has been initialized to 0 (probably simpler than doing math on i
).
如果您希望将行数组表示为对象,请将jsonArr.push(obj)更改为jsonObj[+ rowIx] = obj,其中jsonObj之前被初始化为{},rowIx是一个被初始化为0的计数器(可能比在i上做数学题要简单)。
The expected output you posted is not valid JSON, so I'm assuming that it's supposed to be wrapped by another set of braces:
您发布的预期输出不是有效的JSON,因此我假设它应该由另一组大括号包装:
document.write("<br>"+JSON.stringify({"Values": jsonObj}));
As for saving to a file, this is tricky to do in a portable manner. There are several other answers that address this, including the following:
至于保存到文件中,这在可移植的方式中是很棘手的。关于这个问题,还有其他几个答案,包括:
- Javascript: Create and save file
- Javascript:创建和保存文件
- How to make a browser display a "save as dialog" so the user can save the content of a string to a file on his system?
- 如何让浏览器显示“保存为对话框”,以便用户可以将字符串的内容保存到系统中的文件中?