PowerShell 2.0 ConvertFrom-Json 和 ConvertTo-Json 实现 [英] PowerShell 2.0 ConvertFrom-Json and ConvertTo-Json implementation

查看:23
本文介绍了PowerShell 2.0 ConvertFrom-Json 和 ConvertTo-Json 实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对目前无法升级到 3.0 的 PowerShell 2.0 环境进行猴子补丁.

I would like to monkeypatch a PowerShell 2.0 environment where the upgrade to 3.0 is not possible at this time.

我正在寻找 PowerShell 3.0 中 ConvertFrom-Json cmdlet 和 ConvertTo-Json cmdlet 的 PowerShell 2.0 脚本实现.

I am looking for a PowerShell 2.0 script implementation of the ConvertFrom-Json cmdlet and ConvertTo-Json cmdlet that are in PowerShell 3.0.

我对 ConvertFrom-Json 最感兴趣,但 ConvertTo-Json 也不错.

I am most interested in the ConvertFrom-Json, but ConvertTo-Json would also be nice.

推荐答案

function ConvertTo-Json20([object] $item){
    add-type -assembly system.web.extensions
    $ps_js=new-object system.web.script.serialization.javascriptSerializer
    return $ps_js.Serialize($item)
}

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)
}

如果您收到错误:

Add-Type : Could not load file or assembly 'System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 or one of its dependencies. The system cannot find the file specified. "

... 可以运行这些注册表命令(更多详情):

... these registry commands can be run (more details):

   reg add hklmsoftwaremicrosoft.netframework /v OnlyUseLatestCLR /t REG_DWORD /d 1 /f
   reg add hklmsoftwarewow6432nodemicrosoft.netframework /v OnlyUseLatestCLR /t REG_DWORD /d 1 /f

这篇关于PowerShell 2.0 ConvertFrom-Json 和 ConvertTo-Json 实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆