-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConvertFrom-Json2.ps1
55 lines (50 loc) · 1.33 KB
/
ConvertFrom-Json2.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
function ParseItem($jsonItem)
{
if($jsonItem.PSObject.TypeNames -match 'Array')
{
return ParseJsonArray($jsonItem)
}
elseif($jsonItem.PSObject.TypeNames -match 'Dictionary')
{
return ParseJsonObject([HashTable]$jsonItem)
}
else
{
return $jsonItem
}
}
function ParseJsonObject($jsonObj)
{
$result = New-Object -TypeName PSCustomObject
foreach ($key in $jsonObj.Keys)
{
$item = $jsonObj[$key]
if ($item)
{
$parsedItem = ParseItem $item
}
else
{
$parsedItem = $null
}
$result | Add-Member -MemberType NoteProperty -Name $key -Value $parsedItem
}
return $result
}
function ParseJsonArray($jsonArray)
{
$result = @()
$jsonArray | ForEach-Object -Process {
$result += , (ParseItem $_)
}
return $result
}
function ParseJsonString($json)
{
$config = $javaScriptSerializer.DeserializeObject($json)
return ParseJsonObject($config)
}
$content = Get-Content -Path C:\Temp\kvk12345678.json -Raw
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Web.Extensions")
$result = (New-Object -TypeName System.Web.Script.Serialization.JavaScriptSerializer -Property @{MaxJsonLength=67108864}).DeserializeObject($content)
$ConfigData = ParseItem $result