问题
I would like to monkeypatch a PowerShell 2.0 environment where the upgrade to 3.0 is not possible at this time.
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.
I am most interested in the ConvertFrom-Json, but ConvertTo-Json would also be nice.
回答1:
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)
}
回答2:
I'm unable to comment on the currently top rated post above, however you can run these registry commands (more details):
reg add hklm\software\microsoft\.netframework /v OnlyUseLatestCLR /t REG_DWORD /d 1 /f
reg add hklm\software\wow6432node\microsoft\.netframework /v OnlyUseLatestCLR /t REG_DWORD /d 1 /f
to allow the above ConvertTo-Json20 and ConvertTo-Json20 functions to work if you're getting the error:
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. "
回答3:
Code with javascriptSerializer return objects with Dictionary inside. Modern convertfrom-JSON (4.0+) return objects only. This code transform deserialize object to modern output :)
function Iterate-Tree($jsonTree) {
$result = @()
foreach ($node in $jsonTree) {
$nodeObj = New-Object psobject
foreach ($property in $node.Keys) {
if ($node[$property] -is [System.Collections.Generic.Dictionary[String, Object]] -or $node[$property] -is [Object[]]) {
$inner = @()
$inner += Iterate-Tree $node[$property]
$nodeObj | Add-Member -MemberType NoteProperty -Name $property -Value $inner
} else {
$nodeObj | Add-Member -MemberType NoteProperty -Name $property -Value $node[$property]
#$nodeHash.Add($property, $node[$property])
}
}
$result += $nodeObj
}
return $result
}
function ConvertFrom-Json20{
[cmdletbinding()]
Param (
[parameter(ValueFromPipeline=$true)][object] $PS_Object
)
add-type -assembly system.web.extensions
$PS_JavascriptSerializer=new-object system.web.script.serialization.javascriptSerializer
$PS_DeserializeObject = ,$PS_JavascriptSerializer.DeserializeObject($PS_Object)
#Convert Dictionary to Objects
$PS_DeserializeObject = Iterate-Tree $PS_DeserializeObject
return $PS_DeserializeObject
}
回答4:
I am unable to comment on Edward's answer above. I know this question is older and most people have moved on from it, but I just spent a good amount of time trying to correct the issue seen by other users when using the ConvertTo-JSON20 above.
I was receiving the error below when trying to update a list of sites from a JSON file to an updated list.
"Exception calling "Serialize" with "1" argument(s): "A circular reference was detected while serializing an object of type 'System.Management.Automation.PSMethod'"
When I cast the array by using $array = @() it creates the array as a generic Object[], however, if you explicitly cast the array as a string (by using [string[]]$array = @() ConvertTo-JSON20 is able to convert the string to JSON.
来源:https://stackoverflow.com/questions/28077854/powershell-2-0-convertfrom-json-and-convertto-json-implementation