2016-08-23 93 views
0

我知道使用powershell 3.0这非常容易。唉,我现在正在使用v2。使用powershell 2.0获取json值

所以,说我有一个看起来像这样的JSON文件(config.json):

{ 
    "environment": "dev", 
    "SomeKey": "SomethingElse", 
    "AnotherKey": "More Here", 
    "stuff": { 
     "More": "Stuff Here" 
    }, 
    // More stuff here 
} 

有没有一种方法,我可以得到 “AnotherKey” 的值使用PowerShell(V2.0) ?

更新:
这可能是安全地说,在这种情况下,"AnotherKey":将不会出现在文件的其他地方英寸

+1

您可能想要检查这里的答案http://stackoverflow.com/questions/28077854/powershell-2-0-convertfrom-json-and-convertto-json-implementation –

回答

2

从我的意见给了链接测试:

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