使用 powershell 2.0 获取 json 值
Get a json value using powershell 2.0
我知道使用 powershell 3.0 非常容易。唉,我现在用的是v2。
所以,假设我有一个 json 文件 (config.json),如下所示:
{
"environment": "dev",
"SomeKey": "SomethingElse",
"AnotherKey": "More Here",
"stuff": {
"More": "Stuff Here"
},
// More stuff here
}
有没有办法使用 powershell (v2.0) 获取 "AnotherKey" 的值?
更新:
可以肯定地说,在这种情况下,"AnotherKey":
不会出现在文件的其他任何地方。
根据我在评论中给出的 link 测试:
function ConvertFrom-Json20([object] $item){
add-type -assembly system.web.extensions
$ps_js=new-object system.web.script.serialization.javascriptSerializer
#The comma operator is the array construction operator in PowerShell
return ,$ps_js.DeserializeObject($item)
}
$json = @"
{
"environment": "dev",
"SomeKey": "SomethingElse",
"AnotherKey": "More Here",
"stuff": {
"More": "Stuff Here"
}
}
"@
$a = ConvertFrom-Json20 $json
结果是:
$a.AnotherKey
More Here
注意我不得不编辑你 json 示例(额外的逗号 + 删除注释掉的代码)
我知道使用 powershell 3.0 非常容易。唉,我现在用的是v2。
所以,假设我有一个 json 文件 (config.json),如下所示:
{
"environment": "dev",
"SomeKey": "SomethingElse",
"AnotherKey": "More Here",
"stuff": {
"More": "Stuff Here"
},
// More stuff here
}
有没有办法使用 powershell (v2.0) 获取 "AnotherKey" 的值?
更新:
可以肯定地说,在这种情况下,"AnotherKey":
不会出现在文件的其他任何地方。
根据我在评论中给出的 link 测试:
function ConvertFrom-Json20([object] $item){
add-type -assembly system.web.extensions
$ps_js=new-object system.web.script.serialization.javascriptSerializer
#The comma operator is the array construction operator in PowerShell
return ,$ps_js.DeserializeObject($item)
}
$json = @"
{
"environment": "dev",
"SomeKey": "SomethingElse",
"AnotherKey": "More Here",
"stuff": {
"More": "Stuff Here"
}
}
"@
$a = ConvertFrom-Json20 $json
结果是:
$a.AnotherKey
More Here
注意我不得不编辑你 json 示例(额外的逗号 + 删除注释掉的代码)