我对Windows PowerShell非常陌生。我正在尝试为下面的Json触发ConvertFrom-Json命令:-
$ret=[{"ID":"ABC","type":"Test","code":"AD","enabled":true,"sourceMappings":[{"source":"Test","values":[{"code":"AD","value":"Anderson","enabled":true,"canonicalValue":true,"downStreamDefaultValue":true}]}],"startDate":0,"endDate":0,"updatedBy":"YY","updateDate":1590085877449,"version":4}]我得到了以下输出:-
ID : ABC
type : Test
code : AD
enabled : True
sourceMappings : {@{source=Test; values=System.Object[]}}
startDate : 0
endDate : 0
updatedBy : YY
updateDate : 1590085877449
version : 4如果您注意到分配给System.Object[]的值,.Please请帮助我如何在输出中获得原始值。
发布于 2020-05-23 04:03:41
我认为转换是很好的。
您可以通过以下方式访问这些值:
$tmp = $ret | ConvertFrom-Json
$tmp.sourcemappings
$tmp.sourcemappings.values.code诸若此类
发布于 2020-05-23 14:31:08
让我们首先使您的json文件更具可读性:
{
"ID":"ABC",
"type":"Test",
"code":"AD",
"enabled":true,
"sourceMappings":[{"source":"Test","values":[{"code":"AD","value":"Anderson","enabled":true,"canonicalValue":true,"downStreamDefaultValue":true}]}],
"startDate":0,
"endDate":0,
"updatedBy":"YY",
"updateDate":1590085877449,
"version":4
}其次,您定义json变量的方式是不正确的。在powershell中,您需要定义使用@"和"@包围json内容的json变量。将$ret设置为等于json文件的正确方法如下所示:
$ret=@"
{
"ID":"ABC",
"type":"Test",
"code":"AD",
"enabled":true,
"sourceMappings":[{"source":"Test","values":[{"code":"AD","value":"Anderson","enabled":true,"canonicalValue":true,"downStreamDefaultValue":true}]}],
"startDate":0,
"endDate":0,
"updatedBy":"YY",
"updateDate":1590085877449,
"version":4
}
"@在这种情况下,$ret将被视为string数据类型。然后,您可以使用以下命令转换json:
$var = ConvertFrom-Json -InputObject $ret你现在可以像平常一样与它互动。例如,$var.ID将输出ABC,如果您尝试使用$var.gettype()再次检查对象类型,它将输出:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False PSCustomObject System.Object其中类型为PSCustomObject
似乎我误解了这个问题,是我的错!
它说的原因是
sourceMappings : {@{source=Test; values=System.Object[]}}是因为powershell不想显示整个值,因为值中有许多组件。您可以使用$var.sourceMappings查看sourceMappings类中的值,输出为:
source values
------ ------
Test {@{code=AD; value=Anderson; enabled=True; canonicalValue=True; downStreamDefaultValue=True}}如果你添加了$var.sourceMappings.code等,你可以继续深入查看。
https://stackoverflow.com/questions/61962805
复制相似问题