【问题标题】:Handle html string in JSON in PowerShell在 PowerShell 中处理 JSON 中的 html 字符串
【发布时间】:2020-07-30 14:31:42
【问题描述】:

我有一个包含 HTML 字符串的 JSON 文件,并一直在使用 PowerShell 对其进行操作,但是每次我将其转换回 JSON 时,都会使 HTML 字符串不再相同。

确实需要一些帮助。谢谢! 这是我的 JSON:

{
"count": 1,
"value": [
    {
        "id": 1,
        "fields": {
            
                       "System.Description": "<div><div style=\"margin:0px 0cm 0.000133333px;font-size:11pt;font-family:Calibri, sans-serif;\"><span style=\"font-size:10.0pt;font-family:&quot;Arial&quot;,sans-serif;\"><div><div style=\"margin:0px 0cm 0.000133333px;font-size:11pt;font-family:Calibri, sans-serif;\"><span style=\"font-size:10.0pt;font-family:&quot;Arial&quot;,sans-serif;\"><span style=\"font-family:Arial, sans-serif;background-color:rgb(255, 255, 255);display:inline !important;\">Decision has been made to use HLS video format</span><br></span></div><div style=\"margin:0px 0cm 0.000133333px;font-size:11pt;font-family:Calibri, sans-serif;\"><span style=\"font-size:10.0pt;font-family:&quot;Arial&quot;,sans-serif;\"><span style=\"font-family:Arial, sans-serif;background-color:rgb(255, 255, 255);display:inline !important;\"><br></span></span></div><p style=\"margin:0cm 0cm 8pt;font-size:11pt;font-family:Calibri, sans-serif;margin-bottom:0cm;margin-bottom:.0001pt;text-autospace:none;\"><span style=\"font-size:10.0pt;font-family:&quot;Arial&quot;,sans-serif;\">We\ncan’t use YouTube or Vimeo to serve the videos because analytics required.</span></p></div>We\nneed to use an HTML5 Player in the Desktop web app</span></div><div style=\"margin:0px 0cm 0.000133333px;font-size:11pt;font-family:Calibri, sans-serif;\"><span style=\"font-size:10.0pt;font-family:&quot;Arial&quot;,sans-serif;\"><br></span></div><div style=\"margin:0px 0cm 0.000133333px;font-size:11pt;font-family:Calibri, sans-serif;\"><br></div><div style=\"margin:0px 0cm 0.000133333px;font-size:11pt;font-family:Calibri, sans-serif;\"><br></div></div><br>",

        }
    }
]}

【问题讨论】:

标签: html json string powershell devops


【解决方案1】:

我假设问题是从自定义对象返回 json 时的编码字符。这是一个解决方法:

$json = @'
{
"count": 1,
"value": [
    {
        "id": 1,
        "fields": {
            
                       "System.Description": "<div><div style=\"margin:0px 0cm 0.000133333px;font-size:11pt;font-family:Calibri, sans-serif;\"><span style=\"font-size:10.0pt;font-family:&quot;Arial&quot;,sans-serif;\"><div><div style=\"margin:0px 0cm 0.000133333px;font-size:11pt;font-family:Calibri, sans-serif;\"><span style=\"font-size:10.0pt;font-family:&quot;Arial&quot;,sans-serif;\"><span style=\"font-family:Arial, sans-serif;background-color:rgb(255, 255, 255);display:inline !important;\">Decision has been made to use HLS video format</span><br></span></div><div style=\"margin:0px 0cm 0.000133333px;font-size:11pt;font-family:Calibri, sans-serif;\"><span style=\"font-size:10.0pt;font-family:&quot;Arial&quot;,sans-serif;\"><span style=\"font-family:Arial, sans-serif;background-color:rgb(255, 255, 255);display:inline !important;\"><br></span></span></div><p style=\"margin:0cm 0cm 8pt;font-size:11pt;font-family:Calibri, sans-serif;margin-bottom:0cm;margin-bottom:.0001pt;text-autospace:none;\"><span style=\"font-size:10.0pt;font-family:&quot;Arial&quot;,sans-serif;\">We\ncan’t use YouTube or Vimeo to serve the videos because analytics required.</span></p></div>We\nneed to use an HTML5 Player in the Desktop web app</span></div><div style=\"margin:0px 0cm 0.000133333px;font-size:11pt;font-family:Calibri, sans-serif;\"><span style=\"font-size:10.0pt;font-family:&quot;Arial&quot;,sans-serif;\"><br></span></div><div style=\"margin:0px 0cm 0.000133333px;font-size:11pt;font-family:Calibri, sans-serif;\"><br></div><div style=\"margin:0px 0cm 0.000133333px;font-size:11pt;font-family:Calibri, sans-serif;\"><br></div></div><br>"

        }
    }
]}
'@
$obj = $json | ConvertFrom-Json
# Manipulate $obj in here
[regex]::Replace(($obj | ConvertTo-Json -depth 10),'\\u[0-9a-f]{4}',{param ($m) [regex]::Unescape($m)})

您可以使用 Regex 类方法Unescape() 转换 unicode 字符代码。

【讨论】:

  • 我尝试使用这种方法,但我得到了这个奇怪的字符 ">We\ncan’t use"
  • @art-a-game 里面是卷曲的“智能引用”。您需要保存在 UTF8 中以将替换它的字符保留为正常的撇号' 尝试$obj = [regex]::Unescape(($obj | ConvertTo-Json -Depth 10)) -replace "’", "'" # or even replace with '&amp;apos;' or '&amp;#39;'
  • 我已经尝试过了,但是 JSON 没有正确创建
  • @art-a-game 请在question 中解释您尝试过的内容以及您的意思没有正确创建 显示示例
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-31
  • 1970-01-01
  • 1970-01-01
  • 2014-03-07
  • 2013-12-22
  • 1970-01-01
相关资源
最近更新 更多