相关疑难解决方法(0)

将Unicode字符串转换为转义的ASCII字符串

我该如何转换这个字符串:

This string contains the Unicode character Pi(?)
Run Code Online (Sandbox Code Playgroud)

转换为转义的ASCII字符串:

This string contains the Unicode character Pi(\u03a0)
Run Code Online (Sandbox Code Playgroud)

反之亦然

C#中可用的当前编码将π字符转换为"?".我需要保留那个角色.

c# unicode encoding

75
推荐指数
3
解决办法
10万
查看次数

使用 powershell 正确格式化 JSON

我有一个 JSON 格式的以下文件:

之前ConvertTo-JSON

[
    {
        "Yura": {
            "Cashier": {
                "branch": "release/Retail-v4.0",
                "configuration": "RetailDemo Debug",
                "datetime_deployed": "Apr 18 2018 07:45:05",
                "deployed_by": "anonymous",
                "host": "cashier2-retail4.testing.aws.com",
                "job": "http://jenkins-testing.aws.com:8080/job/CashierDeployment",
                "lineserver": "",
                "messagebus": "",
                "product": "Cashier",
                "publish_profile": "cashier2.retail.dev.pubxml"
            },
            "ContentManager": {
                "branch": "release/Retail-v3.31.1",
                "configuration": "RetailDemo Debug",
                "datetime_deployed": "Jan 17 2018 11:59:24",
                "deployed_by": "anonymous",
                "host": "contentmanager2-retail3.testing.aws.com",
                "job": "http://jenkins-testing.aws.com:8080/job/ContentManagerDeployment",
                "lineserver": "",
                "messagebus": "",
                "product": "ContentManager",
                "publish_profile": "..\\ContentManager.PublishProfiles\\contentmanager2.retail5.dev.pubxml"
            }
        }
    }
]
Run Code Online (Sandbox Code Playgroud)

使用此代码操作数据后:

$json = Get-Content 'D:\script\test.json'  -encoding utf8 | ConvertFrom-Json
$json.yura.ContentManager.branch = 'test'
Run Code Online (Sandbox Code Playgroud)

我将 …

powershell formatting json

6
推荐指数
4
解决办法
1万
查看次数

Json编码HTML字符串

我目前正在从PowerShell脚本生成一个JSON文件,但它输出的是Unicode而不是特殊字符,例如'<'我在LinkText中需要HTML,但不知道如何更改编码.

这是我得到的输出:

[
    {
        "Id":  "187303",
        "LinkText":  "\u003cb style =color:#d11717;\u0027\u003eAnnual General Meeting (MEET)"
    },
    {
        "Id":  "187305",
        "LinkText":  "\u003cb style =color:#d11717;\u0027\u003eAnnual General Meeting (MEET)"
    }
]
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的代码:

$(foreach ($row in $DataSet.Tables[0].Rows){  
    $stockShortName = $row[0].ToString().Trim()
    $id = $row[0].ToString().Trim()
    $linkText = "<b style =color:`#d11717;'>$event_description" 

    (New-Object PSObject |
     Add-Member -PassThru NoteProperty Id $id |
     Add-Member -PassThru NoteProperty LinkText $linkText 
    )
}) | ConvertTo-JSON | Out-File $OutputFile -Encoding "default"
Run Code Online (Sandbox Code Playgroud)

powershell json powershell-3.0

5
推荐指数
1
解决办法
2577
查看次数

带有特殊字符的ConvertTo-Json带有或不带有Unscape-结果错误

我有一个文件,其中包含一些属性,其中某些属性的值包含转义符,例如一些Urls和Regex模式。

在读取内容并转换回json时,无论是否进行转义,内容都不正确。如果我使用转义转义转换回json,则某些正则表达式会中断;如果我使用转义转义转换,则url和某些正则表达式会中断。

我该如何解决这个问题?

最少完整的可验证示例

以下是一些简单的代码块,可让您简单地重现该问题:

内容

$fileContent = 
@"
{
    "something":  "http://domain/?x=1&y=2",
    "pattern":  "^(?!(\\`|\\~|\\!|\\@|\\#|\\$|\\||\\\\|\\'|\\\")).*"
}
"@
Run Code Online (Sandbox Code Playgroud)

随着逃脱

如果我阅读了内容,然后使用以下命令将内容转换回json:

$fileContent | ConvertFrom-Json | ConvertTo-Json | %{[regex]::Unescape($_)}
Run Code Online (Sandbox Code Playgroud)

输出(错误)将是:

{
    "something":  "http://domain/?x=1&y=2",
    "pattern":  "^(?!(\|\~|\!|\@|\#|\$|\||\\|\'|\")).*"
}
Run Code Online (Sandbox Code Playgroud)

没有逃脱

如果我阅读了内容,然后使用以下命令将内容转换回json:

$fileContent | ConvertFrom-Json | ConvertTo-Json 
Run Code Online (Sandbox Code Playgroud)

输出(错误)将是:

{
    "something":  "http://domain/?x=1\u0026y=2",
    "pattern":  "^(?!(\\|\\~|\\!|\\@|\\#|\\$|\\||\\\\|\\\u0027|\\\")).*"
}
Run Code Online (Sandbox Code Playgroud)

powershell

5
推荐指数
1
解决办法
2491
查看次数

标签 统计

powershell ×3

json ×2

c# ×1

encoding ×1

formatting ×1

powershell-3.0 ×1

unicode ×1