html - Powershell JSON 对象到 HTML 表并将其添加到另一个 JSON 对象的值
问题描述
我有以下动态创建的 JSON 对象,可以在名称中包含特殊字符,例如姓氏中的 O'Brian 和 Notes 中的特殊字符。
$jsonData = @"
{
"ReqId": 37,
"First Name": "John",
"Middle Name": "",
"Last Name": "O'Brian",
"Preferred Name": "John",
"Date": "04/28/2021 05:00:00",
"Email": "test@xyz.com",
"Notes": "This is a note with Special characters"
}
"@ | ConvertTo-Json;
我想使用这个 JSON 数据并将其转换为 HTML 表格格式。然后将此 HTML 数据作为另一个 JSON 对象中的键值传递,然后将其发送到 API。
$convData = ConvertTo-Html -InputObject ($jsonData);
$apiJson = @"
{
"Title" : "This is Test",
"Description" : "This is Test Description.<br ><br /> $convData"
}
"@
$apiJson 需要是有效的 JSON,以便 API 可以成功地将其解析为 JSON 对象。
我尝试了 ConvertTo-HTML 方法,但它不起作用并给出了空表。我也想要<table>...</table>
一部分,需要删除其他标签。
{
"Test" : "This is Test",
"description" : "This is Test.<br > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//E
N" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/x
html"> <head> <title>HTML TABLE</title> </head><body> <table> <colgroup><col/></colgroup> <tr>
<th>*</th></tr> <tr><td>539</td></tr> </table> </body></html>"
}
需要帮忙。谢谢你。
解决方案
更改
ConvertTo-Json
为ConvertFrom-Json
- 您想要将 json 转换为可以转换为 html 表的对象将
-Fragment
开关添加到ConvertTo-Html
以仅生成 HTML 表
_
$jsonData = @'
{
"ReqId": 37,
"First Name": "John",
"Middle Name": "",
"Last Name": "O'Brian",
"Preferred Name": "John",
"Date": "04/28/2021 05:00:00",
"Email": "test@xyz.com",
"Notes": "This is a note with Special characters"
}
'@ | ConvertFrom-Json
$convData = $jsonData | ConvertTo-Html -Fragment
$apiJson = @"
{
"Title" : "This is Test",
"Description" : "This is Test Description.<br ><br /> $convData"
}
"@
推荐阅读
- git - 在 ssh-agent 中同时加载两个 ssh-key 时,克隆(更新)git 子模块失败
- python - 诅咒不显示窗口
- sql - 如何在不同表中对具有相同 ID 号的一个位置求和
- c - 如何使用“libnm”库中的函数 nm_connection_get_connection_type 编写 ac 代码来检测连接类型
- javascript - 从 HTML 文件导入 CSS 作为外部 CSS
- r - 通过命令更改 R 版本
- python - 如何在 Python 中从没有 pandas 的文件中获取数据?
- python - XML到python,保存变量以进行进一步优化
- xml - XSLT 用于从一个 xml 读取属性并替换为另一个 xml
- amazon-sqs - 公共交通 :: IConsumer
> 无法使用故障消息并创建了额外的新故障主题