Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.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
使用powershell 2.0获取json值_Powershell_Powershell 2.0 - Fatal编程技术网

使用powershell 2.0获取json值

使用powershell 2.0获取json值,powershell,powershell-2.0,Powershell,Powershell 2.0,我知道使用powershell 3.0非常简单。唉,我现在正在使用v2 因此,假设我有一个json文件(config.json),它如下所示: { "environment": "dev", "SomeKey": "SomethingElse", "AnotherKey": "More Here", "stuff": { "More": "Stuff Here" }, // More stuff here } 是否有办法使用po

我知道使用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”:
不会出现在文件的其他任何地方。

通过我在评论中给出的链接进行测试:

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示例(额外的逗号+删除了注释掉的代码)

从我在注释中给出的链接测试:

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示例(额外的逗号+删除了注释掉的代码)

您可能需要在此处检查答案您可能需要在此处检查答案