powershell 调用Web请求内容转换自Json:从JSON转换失败,错误为:分析值时遇到意外字符(CR;低频)

nuypyhwy  于 2023-01-20  发布在  Shell
关注(0)|答案(2)|浏览(145)

我成功地向网站发送了Web请求并得到了响应:

$Uri = "https://URLXYZ"

$Method = "POST"

$Header = @{
    "Accept" = "*/*";
    "Connection" = "keep-alive";
    "Accept-Encoding" = "gzip, deflate, br";
    "Content-Type" = "text/json; charset=""UTF-8"""
}

$Body = @"
{
    "Items": [
        {
            "Type": "XX",
            "Value": "YY",
        }
    ],
    "TypeId": XY,
    "LiveConditions": []
}
"@

$webrequest = Invoke-WebRequest -Uri $Uri -Body $Body -Headers $Header -Method $Method

现在,我尝试将其从JSON转换,但没有成功:

$webrequest.Content | ConvertFrom-Json
ConvertFrom-Json: Conversion from JSON failed with error: Unexpected character encountered while parsing value: . Path '', line 0, position 0.

当我将输出($webrequest.Content)复制到记事本++时,我可以看到一个回车(CR)和换行符(LF):

$Web请求内容

[{"MailG":[{"DisplayName":{"7":"Worker","9":"Person"},"Mails":"max.mustermann@gmai.com;"}],"ResultCount":1,"Rows":[{"ElementGroups":[{"ObjectContainer":{"Id":55
6677889900,"UID":"1122334455","Info":[],"PreCalculatedSelectedProperties":{"11":"Mustermann","22":"","33":"StreetName","44":"","55":"max.mustermann@gmai.com","66":"","77":"Berlin","88":"","99":"Max Mustermann","00":"+49 00 000 000","111":"userid","222":"xyz","333":"company","444":"1122334455","555":"roomnumber","666":"Germany","777":"Team","888":"+49 000 0 00 0","999":"Max","000":""},"TID":5,"RuleConditionPartResults":{"1448925":false,"1448926":false,"1448927":false,"1448928":false,"1448929":false,"1448930":false,"1448931":false,"1448932":false,"1448933":false,"1448934":false,"1448935":false,"1448936":false,"1448937":false,"1448938":false,"1448939":false,"1448940":false,"1448941":false,"1448942":false},"Img":{"3714":["picture"]},"Parents":[],"Childs":[],"UpObjects":0,"Down":0,"LinkCount":0,"FootObject":{},"BoxIds":[],"DisplayValue":"Max Mustermann","Key":"1122334455"},"Columns":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]}],"Blank":{},"Score":0.0,"SInd":0}],"Page":0,"TID":5}]

这是非预期原因 CR;例如,LF 位于电子邮件地址的中间。如果回复较长,则有多个 CR;LF 在Web请求的内容中。$webrequest.content是 * 类型名称:系统字符串 *
如果我手动删除 CR;记事本++和PrettyPrint中的CF 正在运行:

[
    {
        "MailG": [
            {
                "DisplayName": {
                    "7": "Worker",
                    "9": "Person"
                },
                "Mails": "max.mustermann@gmai.com;"
            }
        ],
        "ResultCount": 1,
        "Rows": [
            {
                "ElementGroups": [
                    {
                        "ObjectContainer": {
                            "Id": 556677889900,
                            "UID": "1122334455",
                            "Info": [],
                            "PreCalculatedSelectedProperties": {
                                "11": "Mustermann",
                                "22": "",
                                "33": "StreetName",
                                "44": "",
                                "55": "max.mustermann@gmai.com",
                                "66": "",
                                "77": "Berlin",
                                "88": "",
                                "99": "Max Mustermann",
                                "00": "+49 00 000 000",
                                "111": "userid",
                                "222": "xyz",
                                "333": "company",
                                "444": "1122334455",
                                "555": "roomnumber",
                                "666": "Germany",
                                "777": "Team",
                                "888": "+49 000 0 00 0",
                                "999": "Max",
                                "000": ""
                            },
                            "TID": 5,
                            "RuleConditionPartResults": {
                                "1448925": false,
                                "1448926": false,
                                "1448927": false,
                                "1448928": false,
                                "1448929": false,
                                "1448930": false,
                                "1448931": false,
                                "1448932": false,
                                "1448933": false,
                                "1448934": false,
                                "1448935": false,
                                "1448936": false,
                                "1448937": false,
                                "1448938": false,
                                "1448939": false,
                                "1448940": false,
                                "1448941": false,
                                "1448942": false
                            },
                            "Img": {
                                "3714": [
                                    "picture"
                                ]
                            },
                            "Parents": [],
                            "Childs": [],
                            "UpObjects": 0,
                            "Down": 0,
                            "LinkCount": 0,
                            "FootObject": {},
                            "BoxIds": [],
                            "DisplayValue": "Max Mustermann",
                            "Key": "1122334455"
                        },
                        "Columns": [
                            0,
                            1,
                            2,
                            3,
                            4,
                            5,
                            6,
                            7,
                            8,
                            9,
                            10,
                            11,
                            12,
                            13,
                            14,
                            15,
                            16,
                            17
                        ]
                    }
                ],
                "Blank": {},
                "Score": 0.0,
                "SInd": 0
            }
        ],
        "Page": 0,
        "TID": 5
    }
]

我已经尝试转换它,甚至取代CR;在PowerShell中LF它没有成功。
我发现的唯一的变通方法是将内容写入. txt文件,然后再次读取。

$webrequest.Content | Out-File "C:\Temp\WebRequestTemp.txt"
$json = Get-Content "C:\Temp\WebRequestTemp.txt" | ConvertFrom-Json

之后,我能够将其从JSON转换并处理数据。

为什么不能像往常一样直接从JSON转换?

6tr1vspr

6tr1vspr1#

为了帮助你得到更详细的答案。分享网络回复的内容是很有用的。但是也许下面的一行会解决你的问题。

$jsonCorrected = [Text.Encoding]::UTF8.GetString([Text.Encoding]::GetEncoding(28591).GetBytes(($webrequest.Content)))
0md85ypi

0md85ypi2#

我也遇到过类似的问题,问题是在内容块的开头有一个未知的隐形字符,这个字符是“U+FEFF”。
我设法通过执行$webrequest.Content -replace '\uFEFF' | ConvertFrom-Json将其替换
我希望这对其他人也有效。

相关问题