如何将动态变化的文本文件转为规范JSON并生成HTML?
解决方法:将扁平JSON转换为结构化数据并生成动态HTML
第一步:把扁平JSON转换成结构化JSON
你的原始JSON是扁平字符串数组,按「模块标题+内容」的形式排列,我们先将其解析为更易处理的结构化对象,PowerShell脚本实现如下:
# 读取原始JSON文件 $flatJson = Get-Content -Path "path/to/your/input.json" -Raw | ConvertFrom-Json # 初始化结构化数据容器 $structuredData = [PSCustomObject]@{ "Virtual Machines" = @() "Data Disk" = @() "Network Interfaces" = @() "Current Image ID used" = $null "Disk Encryption Status" = @() "Disk Encryption Set" = $null } $currentSection = $null foreach ($item in $flatJson) { # 跳过空字符串 if ([string]::IsNullOrWhiteSpace($item)) { continue } # 识别模块标题,切换当前处理分区 if ($structuredData.PSObject.Properties.Name -contains $item) { $currentSection = $item continue } # 处理磁盘加密状态的嵌套结构 if ($currentSection -eq "Disk Encryption Status") { # 当前项是VM名称,初始化加密状态对象 if (-not $item.Contains(":")) { $vmEncryption = [PSCustomObject]@{ VMName = $item Details = @{} } $structuredData."Disk Encryption Status" += $vmEncryption } else { # 解析键值对并存入对应VM的详情 $keyValue = $item -split ":", 2 | ForEach-Object { $_.Trim() } $vmEncryption.Details[$keyValue[0]] = $keyValue[1] } continue } # 处理普通列表或单值模块 if ($currentSection -in "Current Image ID used", "Disk Encryption Set") { $structuredData.$currentSection = $item } else { $structuredData.$currentSection += $item } } # 输出结构化JSON(可选,用于验证) $structuredData | ConvertTo-Json -Depth 3 | Out-File "path/to/structured-output.json"
这段脚本会自动归类各模块内容,把磁盘加密状态整理为「VM名称+属性键值对」的嵌套结构,适配后续动态数据变化。
第二步:从结构化数据生成动态HTML
基于结构化数据生成自适应HTML,不管VM、磁盘数量如何变化,页面都会自动渲染所有内容,PowerShell脚本如下:
# 读取结构化JSON(若第一步已生成文件,直接读取;否则可直接使用$structuredData对象) $structuredData = Get-Content -Path "path/to/structured-output.json" -Raw | ConvertFrom-Json # 构建HTML内容 $htmlContent = @" <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>资源组信息报表</title> <style> body { font-family: Arial, sans-serif; margin: 20px; } .section { margin-bottom: 30px; } .section h2 { color: #2c3e50; border-bottom: 2px solid #3498db; padding-bottom: 5px; } .list-item { margin: 5px 0; padding: 8px; background-color: #f8f9fa; border-radius: 4px; } .vm-encryption { margin-left: 20px; margin-top: 10px; border: 1px solid #ddd; padding: 10px; border-radius: 4px; } .vm-encryption h3 { color: #34495e; margin-top: 0; } .detail-row { margin: 3px 0; } .detail-key { font-weight: bold; display: inline-block; width: 300px; } </style> </head> <body> <h1>资源组信息</h1> "@ # 动态生成虚拟机模块 $htmlContent += @" <div class="section"> <h2>虚拟机</h2> "@ foreach ($vm in $structuredData."Virtual Machines") { $htmlContent += " <div class='list-item'>$vm</div>`n" } $htmlContent += " </div>`n" # 动态生成数据磁盘模块 $htmlContent += @" <div class="section"> <h2>数据磁盘</h2> "@ foreach ($disk in $structuredData."Data Disk") { $htmlContent += " <div class='list-item'>$disk</div>`n" } $htmlContent += " </div>`n" # 动态生成网络接口模块 $htmlContent += @" <div class="section"> <h2>网络接口</h2> "@ foreach ($nic in $structuredData."Network Interfaces") { $htmlContent += " <div class='list-item'>$nic</div>`n" } $htmlContent += " </div>`n" # 生成镜像ID模块 $htmlContent += @" <div class="section"> <h2>当前使用的镜像ID</h2> <div class='list-item'>$($structuredData."Current Image ID used")</div> </div>`n "@ # 动态生成磁盘加密状态模块 $htmlContent += @" <div class="section"> <h2>磁盘加密状态</h2> "@ foreach ($encryption in $structuredData."Disk Encryption Status") { $htmlContent += " <div class='vm-encryption'>`n" $htmlContent += " <h3>$($encryption.VMName)</h3>`n" foreach ($key in $encryption.Details.Keys) { $htmlContent += " <div class='detail-row'><span class='detail-key'>$key</span>: $($encryption.Details[$key])</div>`n" } $htmlContent += " </div>`n" } $htmlContent += " </div>`n" # 生成磁盘加密集模块 $htmlContent += @" <div class="section"> <h2>磁盘加密集</h2> <div class='list-item'>$($structuredData."Disk Encryption Set")</div> </div> </body> </html> "@ # 输出HTML文件 $htmlContent | Out-File "path/to/resource-group-report.html" -Encoding UTF8
脚本说明
- 结构化处理阶段:自动识别模块边界,将扁平数据归类为分层结构,解决原始JSON无固定格式的问题。
- HTML生成阶段:通过遍历结构化数据的属性,动态渲染页面元素,后续VM、磁盘数量变化时无需修改脚本。
- 内置基础CSS样式,可根据需求调整页面布局和外观。
内容的提问来源于stack exchange,提问作者Rojay C
相关产品推荐
相关产品推荐

