Reputation: 9589
I have a System.Collections.Generic.Dictionary that I have obtained by running the code snippet below. How do I convert $Obj to a PSCustomObject?
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions")
$jsonserial= New-Object -TypeName System.Web.Script.Serialization.JavaScriptSerializer
$jsonserial.MaxJsonLength = 67108864
$Obj = $jsonserial.DeserializeObject($JsonString)
Upvotes: 5
Views: 6086
Reputation: 40838
New-Object
should work from PS 2.0 onwards:
$custom = new-object PSCustomObject -Property $obj
Upvotes: 3
Reputation: 68331
This seems to work:
$ht = @{}
$ht += $obj
[PSCustomObject]$ht
Edit:
V3 solution:
$ht = [collections.hashtable]$obj
[PSCustomObject]$ht
Upvotes: 3
Reputation: 9589
I ended up using the following after the last line in the question
$Obj | ForEach-Object {
$props = @{}
$_.GetEnumerator() | ForEach-Object {
$props[$_.Key] = $_.Value
}
[PSCustomObject]$props
}
If there is a faster, shorter or better way, please feel free to answer
Upvotes: 3