PowerShell中的Json数据转换cmdlets

来源:互联网 发布:生物医学数据库官网 编辑:程序博客网 时间:2024/06/06 01:50
早在PowerShell 3.0的时候就被引入了两个新的cmdlet命令,一个是ConvertFrom-Json 和ConvertTo-Json ,虽然目前看来过了很久,因为最近有需要处理到Json的数据,既然PowerShell已经支持Json数据了,因此,我觉得可以拿PowerShell对Json做些数据处理。就以这两个命令作为例子开始吧。
先说说ConvertTo-Json这个命令,从字面我们可以看出来它应该是一个把对象转换为一种Json格式的命令,一起来看一下帮助文档简介:
Detailed DescriptionThe ConvertTo-Json cmdlet converts any object to a string in JavaScript Object Notation (JSON) format. The properties are converted to field names, the field values are converted to property values, and the methods are removed.You can then use the ConvertFrom-Json cmdlet to convert a JSON-formatted string to a JSON object, which is easily managed in Windows PowerShell.Many web sites use JSON instead of XML to serialize data for communication between servers and web-based apps.
我们可以从简介中看出,ConvertTo-Json 命令可以把任何对象字符转换到JSON格式的数据。那么如下我们试着把一些对象试着用这个命令转换为JSON格式看看:
PS C:\Users\Administrator> Get-Process | Select-Object -Property ProcessName|ConvertTo-Jso[    {        "ProcessName":  "conhost"    },    {        "ProcessName":  "conhost"    },    {        "ProcessName":  "csrss"    },    {        "ProcessName":  "csrss"    },    {        "ProcessName":  "dllhost"    },    {        "ProcessName":  "dwm"    },    {        "ProcessName":  "explorer"    },    {        "ProcessName":  "Idle"    },    {        "ProcessName":  "iexplore"    },    {        "ProcessName":  "iexplore"    },    {        "ProcessName":  "IpOverUsbSvc"    },    {        "ProcessName":  "lsass"    },    {        "ProcessName":  "msdtc"    },    {        "ProcessName":  "powershell"    },    {        "ProcessName":  "powershell_ise"    },    {        "ProcessName":  "services"    },    {        "ProcessName":  "smss"    },    {        "ProcessName":  "spoolsv"    },    {        "ProcessName":  "sqlwriter"    },    {        "ProcessName":  "svchost"    },    {        "ProcessName":  "svchost"    },    {        "ProcessName":  "svchost"    },    {        "ProcessName":  "svchost"    }]
如上这里我们用Get-Process结合Select-Object命令提取出Name属性列表,因为前面提到ConvertTo-Json命令可以把任何对象都转换为JSON格式数据,所以我们接着把提取出来的PowerShell对象数据通过管道传递给ConvertTo-Json然后转换很顺利的得到了JSON格式的数据。
接着我们要说下另一个相对的命令ConvertFrom-Json,依然看一下帮助的简介:
Detailed DescriptionThe ConvertFrom-Json cmdlet converts a JSON-formatted string to a custom object (PSCustomObject) that has a property for each field in the JSON string. JSON is commonly used by web sites to provide a textual representation of objects.To generate a JSON string from any object, use the ConvertTo-Json cmdlet.
该命令可以把JSON格式的数据转换PowerShell的自定义数据对象,因此为能够很好的说明这个问题,我们把之前转换得到的数据保存赋值给一个变量,接着再用这个命令试着转换看看是否能得到我们预期想要的结果,操作如下:
$JSONProcess = Get-Process | Select-Object -Property ProcessName|ConvertTo-Json
接着我们把我们赋值的变量对象传递给ConvertFrom-Json命令,可以发现很平滑顺利的又得到了我们想要的数据对象。
PS C:\Users\Administrator> ConvertFrom-Json -InputObject $JSONProcessProcessName-----------conhostconhostcsrsscsrssdllhostdwmexplorerIdleiexploreiexploreIpOverUsbSvclsassmsdtcpowershellpowershell_iseservicessmssspoolsvsqlwritersvchostsvchostsvchostsvchostsvchostsvchostsvchostsvchostSystemtaskhostexTPAutoConnectTPAutoConnSvcvmtoolsdvmtoolsdVMToolsHookProcwininitwinlogon





0 0
原创粉丝点击