html - 在 Power shell 中创建具有多行和多列的动态 HTML 表
问题描述
参考示例 JSON,我正在尝试创建动态 HTML 表,但是我无法获得具有特定行数据的特定列名。
条件:
根据 JSON 示例,我有“Table_0”和“Table_1”表名,仅对于我需要转换为 HTML 的第一个表数据。没有更多的其他表。这就是我的脚本中我只访问的原因
$dataTable = ($response | ConvertFrom-Json).Tables[0]
在我的示例 JSON 中,我只有 2 列,但实际上我可能有一个或多个列,其中包含行中的相应数据
列名不固定。在 JSON 的另一个响应中,我可能有不同的列(单列或多列),但相应列的数据将可用。比如说 Ex: another JSON ,我有 4 列,所以我的输出应该在 HTML 表中显示 4 列与该行数。
JSON 文件格式是固定的,每个响应只会添加/删除列和行。
脚本:
$response = get-content <JSONFilePath>
$dataTable = ($response | ConvertFrom-Json).Tables[0]
$columncount = $dataTable.Columns.count
$header = "<table border=1><tr>"
for($i=0;$i -lt $columncount;$i++){
$header = $header+"<th>"+$dataTable.Columns[$i].ColumnName+"</th>"
}
$header = $header +"</tr>"
$Lines ="<tr>"
for($i=0;$i -lt $columncount;$i++)
{
$val=""
foreach($rowdata in $dataTable.Rows)
{
###Unable to get Specific row data for specific Column name
$val = $val+"<td>"+$i[$rowdata]+"</td>"
}
$Lines = $Lines+ $val +"</tr>"
}
$tableval = $header+ $Lines +"</table>"
Write-Output $tableval
JSON:
{
"Tables": [
{
"TableName": "Table_0",
"Columns": [
{
"ColumnName": "Name",
"DataType": "String",
"ColumnType": "string"
},
{
"ColumnName": "Rating",
"DataType": "Double",
"ColumnType": "real"
}
],
"Rows": [
[
"Satya",
1000.0
],
[
"Pavan",
2000.00
]
]
},
{
"TableName": "Table_1",
"Columns": [
{
"ColumnName": "Value",
"DataType": "String",
"ColumnType": "string"
}
],
"Rows": [
[
]
]
}
]
}
输出:
<table>
<tr>
<th>Name</th>
<th>Rating</th>
</tr>
<tr>
<td>Satya</td>
<td>1000</td>
</tr>
<tr>
<td>Pavan</td>
<td>2000</td>
</tr>
</table>
解决方案
推荐阅读
- openmdao - 在 OpenMDAO 中使用 CaseReader 的 ModuleNotFoundError
- delphi - FMX:如何创建无法最大化的 ToolWindow 表单?
- c++ - 使用整个堆栈内存
- python - 使用 plt.annotate 时调整文本错误
- python - 如何在多个 relplot 图表上叠加 pyplot 视觉元素
- jenkins - 如何在管道中使用多分支管道文件夹属性?
- python - 计算熊猫时间窗口中列值重复的次数
- snakemake - 如何定位包含通配符的中间 Snakemake 规则
- xamarin.ios - FFImageLoading 在发布模式 Xamarin.iOS 中不显示 webp 图像
- git - 我的头和主人是分开的(git)。我怎样才能重新加入他们?