我有一个包含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:"Arial",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:"Arial",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:"Arial",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:"Arial",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:"Arial",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>",
}
}
]}
我假设问题是从自定义对象返回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:"Arial",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:"Arial",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:"Arial",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:"Arial",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:"Arial",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字符代码。