我成功地向网站发送了一个网络请求并得到了回复:
$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)复制到 Notepad++ 时,我可以看到回车(CR)和换行(LF):
$webrequest.Content
[{"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例如位于电子邮件地址的中间。如果响应较长,则webrequest 的内容中有多个CR;LF 。$webrequest.content 是TypeName: System.String
如果我在 Notepad++ 和 PrettyPrint 中手动删除CR;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
}
]
我已经尝试在 PowerShell 中转换它甚至替换 CR;LF 它但没有成功。
我发现的唯一解决方法是将内容写入 .txt 文件并再次读取。
$webrequest.Content | Out-File "C:\Temp\WebRequestTemp.txt"
$json = Get-Content "C:\Temp\WebRequestTemp.txt" | ConvertFrom-Json
之后,我能够将其从 JSON 转换并处理数据。
为什么我不能像往常一样直接从 JSON 转换它?