在 Powershell 中检索 system.object 基本类型的字符串值
Retrieving String value of system.object basetype in Powershell
我有来自 http post 请求的结果,我将内容存储在变量中。
$response = Invoke-WebRequest -Uri "https://xxxxxx" -Method Post -Body $Body -Headers $header
$Content =$response.Content
内容如下所示:
{"id":"246584121546545124545"}
现在我只想要 id 的值。我尝试使用 select 对象和 属性 参数来获取 id 的值。但它不起作用,因为内容是一个字符串。看起来很简单,但找不到检索值的方法。
PS C:\Scripts> $Content.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
尝试从 JSON 解析为自定义 PS 对象,然后访问 id
属性:
> $x = ConvertFrom-Json -InputObject $Content
> $x.id
您也可以使用Invoke-RestMethod
。
$response = Invoke-RestMethod -Uri "https://xxxxxx" -Method Post -Body $Body -Headers $header
$response.id
调用-RestMethod
The Invoke-RestMethod
cmdlet sends HTTP and HTTPS requests to Representational State Transfer (REST) web services that returns richly structured data.
Windows PowerShell formats the response based to the data type.
For JavaScript Object Notation (JSON) or XML, Windows PowerShell converts (or deserializes) the content into objects.
我有来自 http post 请求的结果,我将内容存储在变量中。
$response = Invoke-WebRequest -Uri "https://xxxxxx" -Method Post -Body $Body -Headers $header
$Content =$response.Content
内容如下所示:
{"id":"246584121546545124545"}
现在我只想要 id 的值。我尝试使用 select 对象和 属性 参数来获取 id 的值。但它不起作用,因为内容是一个字符串。看起来很简单,但找不到检索值的方法。
PS C:\Scripts> $Content.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
尝试从 JSON 解析为自定义 PS 对象,然后访问 id
属性:
> $x = ConvertFrom-Json -InputObject $Content
> $x.id
您也可以使用Invoke-RestMethod
。
$response = Invoke-RestMethod -Uri "https://xxxxxx" -Method Post -Body $Body -Headers $header
$response.id
调用-RestMethod
The
Invoke-RestMethod
cmdlet sends HTTP and HTTPS requests to Representational State Transfer (REST) web services that returns richly structured data.
Windows PowerShell formats the response based to the data type.
For JavaScript Object Notation (JSON) or XML, Windows PowerShell converts (or deserializes) the content into objects.