简体   繁体   English

将控制台应用程序的json输出转换为PowerShell对象

[英]Converting the json output of a console app to a PowerShell object

Suppose I have a foo.exe console app that returns some json, and I want to turn that into a PowerShell object. 假设我有一个foo.exe控制台应用程序,该应用程序返回一些json,并且我想将其转换为PowerShell对象。

I was hoping to simply write: 我希望简单地写:

$o = foo.exe | ConvertFrom-Json

But this doesn't work because the output of the console app is treated as an Array instead of a string. 但这不起作用,因为控制台应用程序的输出被视为数组而不是字符串。 I can instead write: 我可以改写:

$o = ([string]foo.exe) | ConvertFrom-Json

which feels dirtier than I was hoping. 感觉比我希望的还要脏。

Question : can I do better than what I have above to get a PowerShell object out of the output of a console app? 问题 :从控制台应用程序的输出中获取PowerShell对象,我能做得比上面更好吗?

作为选择:

$o = foo.exe | Out-String | ConvertFrom-Json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM