我有这个 JSON:
'{
"fibo": {
"2": { "hi": "there" },
"8": { "and": "you?" }
"5": { "well": "doing fine" },
"3": { "what": "is hap?" },
}
}'
Run Code Online (Sandbox Code Playgroud)
例如,我想以{ "what": "is hap?" }
最简单的方式提取最后一个元素(对象)(最好是带管道的单行,而不用foreach
-ing 它)。
我试过:
'{
"fibo": {
"2": { "hi": "there" },
"8": { "and": "you?" }
"5": { "well": "doing fine" },
"3": { "what": "is hap?" },
}
}' | ConvertFrom-Json | select { $_.fibo } | select -Last 1
Run Code Online (Sandbox Code Playgroud)
但select -Last 1
似乎什么也没做。
我可以使用select { $_.fibo. }
哪个有效,但明天该对象可能会更改为例如:
'{
"fibo": {
"2": { "hi": "there" },
"8": { "and": "you?" }
"5": { "well": "doing fine" },
"314": { "what": "is hap?" },
}
}'
Run Code Online (Sandbox Code Playgroud)
所以我不能依赖那个..
select -last 1
给你最后一个“fibo”对象。但是您已经只有一个对象,但有 4 个属性。
要获取属性,您可以使用
$pso = '{
"fibo": {
"2": { "hi": "there" },
"8": { "and": "you?" },
"5": { "well": "doing fine" },
"3": { "what": "is hap?" }
}
}' | ConvertFrom-Json
# gets properties, but does not preserver order:
$pso.fibo | Get-Member -MemberType NoteProperty
# this will preserve the order
$pso.fibo.psobject.properties
# so to get last property (it already has the name and value)
$prop = $pso.fibo.psobject.properties | select -Last 1
$prop
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
418 次 |
最近记录: |