【问题标题】:Problems in reading a commandline json object using powershell使用 powershell 读取命令行 json 对象的问题
【发布时间】:2013-07-11 09:30:22
【问题描述】:

我有一个 powershell 脚本“test.ps1”,它将一个 json 字符串作为命令行输入并尝试解析它。

powershell脚本如下-

param(
 [string]$json = $(throw 'test')
)
$currentPath = Get-Location
[Reflection.Assembly]::LoadFile("$currentPath\Newtonsoft.Json.dll")
$result = [Newtonsoft.Json.Linq.JObject]::Parse($json)
foreach($unit in $result["DevResults"]) {Write-Host $unit.TechnologyName.ToString()}

但它给了我以下错误-

PS C:\Users\aghosh.RJFDEV\Documents> ./test.ps1 '{"DevResults":[{"TechnologyName":"TFS","RuleName":"Alt CI ID for ESB","OutputValue":"ESClientCenter"},{"TechnologyName":"TFS","RuleName":"TFS Team Project Name","OutputValue":"ClientCenter"}],"QaResults":[{"TechnologyName":"TFS","RuleName":"Alt CI ID for ESB","OutputValue":"ESClientCenter"},{"TechnologyName":"TFS","RuleName":"TFS Team Project Name","OutputValue":"ClientCenter"}],"PreProdResults":[{"TechnologyName":"TFS","RuleName":"Alt CI ID for ESB","OutputValue":"ESClientCenter"},{"TechnologyName":"TFS","RuleName":"TFS Team Project Name","OutputValue":"ClientCenter"}],"ProdResults":[{"TechnologyName":"TFS","RuleName":"Alt CI ID for ESB","OutputValue":"ESClientCenter"},{"TechnologyName":"TFS","RuleName":"TFS Team Project Name","OutputValue":"ClientCenter"},{"TechnologyName":"TFS","RuleName":"Process Template","OutputValue":"Raymond James CMMI V3.5"}]}'

GAC    Version        Location                                                                                                                                                                                                                                     
---    -------        --------                                                                                                                                                                                                                                     
False  v2.0.50727     C:\Users\aghosh.RJFDEV\documents\Newtonsoft.Json.dll                                                                                                                                                                                         
You cannot call a method on a null-valued expression.
At C:\Users\aghosh.RJFDEV\Documents\test.ps1:13 char:82
+ foreach($unit in $result["DevResults"]) {Write-Host $unit.TechnologyName.ToString <<<< ()}
    + CategoryInfo          : InvalidOperation: (ToString:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At C:\Users\aghosh.RJFDEV\Documents\test.ps1:13 char:82
+ foreach($unit in $result["DevResults"]) {Write-Host $unit.TechnologyName.ToString <<<< ()}
    + CategoryInfo          : InvalidOperation: (ToString:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

我的 powershell 版本是 2.0,而 Json.Net 库版本是 3.5。

请帮助我解决错误或建议任何其他合适的方法。

提前致谢。

【问题讨论】:

    标签: json powershell json.net powershell-2.0


    【解决方案1】:

    改变这一行:

    foreach($unit in $result["DevResults"]) {Write-Host $unit.TechnologyName.ToString()}
    

    到这里:

    foreach($unit in $result["DevResults"]) {Write-Host $unit["TechnologyName"].ToString()}
    

    【讨论】:

    • 嗨,布赖恩...我仍然遇到同样的错误。它没有帮助。但是相同的代码在 Powershell 3.0 中运行得非常好。所以,我正在为 2.0 寻找合适的解决方案。谢谢。
    • 我明白了。我使用 PowerShell 3.0 版进行了测试。不过,恐怕我无法为 2.0 版提供帮助。
    猜你喜欢
    • 2014-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    • 2019-08-19
    • 1970-01-01
    相关资源
    最近更新 更多