Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从json文件中读取特定对象的一个值_Json_Powershell - Fatal编程技术网

从json文件中读取特定对象的一个值

从json文件中读取特定对象的一个值,json,powershell,Json,Powershell,我只想从test.json文件返回一个对象的哈希值 现在,我正在用代码获取所有哈希值 json文件: [ { "name": "abc.txt", "hash": "D23FC7C4C9F1ED7CD147D7D29E3A541D" }, { "name": "def.txt", "hash": "681B75B81

我只想从test.json文件返回一个对象的哈希值

现在,我正在用代码获取所有哈希值

json文件:

[
{
    "name": "abc.txt",
    "hash": "D23FC7C4C9F1ED7CD147D7D29E3A541D"
},
{
    "name": "def.txt",
    "hash": "681B75B81734F7215C2DAD1F7EDFDAF7"
},
{
    "name": "ghi.txt",
    "hash": "81709CDC04EBDBDAA9BA15F6CAF1F05B"
},
{
    "name": "xyz.txt",
    "hash": "56F07815D06966FA3A73275797496881"
}
]
我的代码:

$jsonFile = $PSScriptRoot + "\test.json"
(Get-Content $jsonFile | ConvertFrom-Json | where {$_.name -eq 'abc.txt'}).hash

像这样,json文件包含一个字典列表。首先,您需要选择列表中希望包含的项目,然后可以根据键检索值

试着这样做:

$jsonFile = $PSScriptRoot + "\test.json"
$jsonList = Get-Content $jsonFile | ConvertFrom-Json
$jsonList[0].hash
或者:

$jsonObject = $jsonList | Where-Object {$_.name -eq 'abc.txt'} | Select-Object hash

小心括号:

((Get-Content $jsonFile | ConvertFrom-Json) | where {$_.name -eq "abc.txt"}).hash
#D23FC7C4C9F1ED7CD147D7D29E3A541D