Powershell:将 JSON 个对象转换为 HTML Table?

Powershell: Converting JSON objects to HTML Table?

我有一个包含多个 JSON 个对象的变量。

{
    "Objects":  [
                            {
                                "Name":  "Object 2",
                                "Description":  "This is object 2"
                            },
                            {
                                "Name":  "Object 3",
                                "Description":  "This is object 3"
                            },
}

我想将它们输出到 HTML table:

NAME | DESCRIPTION

Object ... | Object desc ...

我做了以下脚本:

$convData = $jsonObjects | ConvertTo-Html -Fragment
$htmlTable = @"
    {
        "Title" : "HTML Formatted Table",
        "Description" : "This is a table with JSON Objects<br ><br /> $convData"
    }
"@

$htmlTable

但输出是:

{ "Title" : "HTML Formatted Table", "Description" : "This is a table with JSON
Objects<br /><br />
<table>
  <colgroup>
    <col />
    <col />
    <col />
    <col />
    <col />
    <col />
    <col />
  </colgroup>
  <tr>
    <th>Count</th>
    <th>IsReadOnly</th>
    <th>Keys</th>
    <th>Values</th>
    <th>IsFixedSize</th>
    <th>Sy ncRoot</th>
    <th>IsSynchronized</th>
  </tr>
  <tr>
    <td>1</td>
    <td>False</td>
    <td>
      System.Collections.Specialized.OrderedDictionary+OrderedDictionaryKeyValueCollection
    </td>
    <td>
      System.Collections.Specialized.OrderedDictionary+OrderedDictionaryKe
      yValueCollection
    </td>
    <td>False</td>
    <td>System.Object</td>
    <td>False</td>
  </tr>
</table>
" }

拜托,有人可以为我指明正确的方向吗? 谢谢...

这将创建一个 HTML table。在这种方法中,您可以在 html 输出文件中使用所有 CSS 样式。ConvertTo-Html 不会给您这样的选项。

$json = '{
 "Objects":  [
{
"Name":  "Object 2",
"Description":  "This is object 2"
},
{
"Name":  "Object 3",
"Description":  "This is object 3"
}
]}'
$Data=$json|ConvertFrom-Json

#HTML Table header
$html="
<Html>
<Body>
<Table border=1 style='border-collapse: collapse;width:50%;text-align:center'>
<th>Name</th> <th>Description</th> "

#adding HTML table row
foreach($D in $Data.Objects)
{
  $html+= "<tr> <td> $($D.name) </td> <td> $($D.Description) </td></tr>"
}

#Close HTML tags
$html +="</table></body></html>"

#Write data to HTML file
$html >>c:\json.html