我有一个JSON对象,其中一个元素的值是一个字符串.在这个字符串中有字符"<RPC>".我拿这个整个JSON对象,在我的ASP.NET服务器代码中,我执行以下操作来获取命名对象rpc_response并将其添加到POST响应中的数据:
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
HttpContext.Current.Response.AddHeader("Pragma", "no-cache");
HttpContext.Current.Response.AddHeader("Cache-Control", "private, no-cache");
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=\"files.json\"");
HttpContext.Current.Response.Write(serializer.Serialize(rpc_response));
HttpContext.Current.Response.ContentType = "application/json";
HttpContext.Current.Response.StatusCode = 200;
Run Code Online (Sandbox Code Playgroud)
在序列化对象之后,我在另一端(不是Web浏览器)接收它,并且该特定字符串如下所示:\u003cRPC\u003e.
我该怎么做才能防止这些(和其他)字符被正确编码,仍然可以序列化我的JSON对象?
我的问题与这个问题类似:
Json 文件到 powershell 并返回到 json 文件
在 powershell 中导入和导出 ARM 模板时,使用Convert-FromJson和Convert-ToJson引入 unicode 转义序列。
我用这里的代码再次逃脱。
一些示例代码(为了清晰起见,多行):
$armADF = Get-Content -Path $armFile -Raw | ConvertFrom-Json
$armADFString = $armADF | ConvertTo-Json -Depth 50
$armADFString |
ForEach-Object { [System.Text.RegularExpressions.Regex]::Unescape($_) } |
Out-File $outputFile
Run Code Online (Sandbox Code Playgroud)
这是我一直在阅读的 docoUnescape
输出文件的结果是相同的,只是所有文字实例\n(原始 JSON 文件中的)都转换为实际的回车符。这打破了 ARM 模板。
如果我不包含 Unescape 代码,它们\n会被保留,但 unicode 字符也会被保留,这也会破坏 ARM 模板。
看来我需要预先转义,\n所以当我调用 Unescape 时,它们会变成漂亮的小\n。我尝试过一些事情,例如在调用 unescape 之前添加此内容。
$armADFString = $armADFString -replace("\\n","\u000A") …Run Code Online (Sandbox Code Playgroud)