首页 > 解决方案 > 在 Powershell 中检索 system.object 基本类型的字符串值

问题描述

我有来自 http post 请求的结果,我将内容存储在一个变量中。

$response = Invoke-WebRequest -Uri "https://xxxxxx" -Method Post -Body $Body -Headers $header 
$Content =$response.Content

内容如下所示:

{"id":"246584121546545124545"}

现在我只想要 id 的值。我尝试使用选择对象和属性参数来获取 id 的值。但它不起作用,因为内容是一个字符串。看起来很简单,但找不到检索值的方法。

PS C:\Scripts> $Content.GetType()

IsPublic IsSerial Name                                     BaseType                                                                                                                           
-------- -------- ----                                     --------                                                                                                                           
True     True     String                                   System.Object  

标签: powershellpowershell-5.0

解决方案


尝试从 JSON 解析为自定义 PS 对象,然后访问该id属性:

> $x = ConvertFrom-Json -InputObject $Content
> $x.id

推荐阅读