h41msh1c0r
Goto Top

Powershell Json Aufbau Verschachtelung

Aloa in die Runde,

$jsonInhalt = [ordered]@{Allgemein=@{"PfadCfg"='';"PfadLib"=''};   
                         Laufwerke=[ordered]@{}; 
                         Services=[ordered]@{
                                      Hersteller1=[ordered]@{
                                                    Core=[ordered]@{"Dienstname"='';"Status"=''};  
                                                    Runtime=[ordered]@{"Dienstname"='';"Status"=''}  
                                                    }; 
                                      Hersteller2=[ordered]@{
                                                    ManagementAgent=[ordered]@{}
                                                    }
                                     }
                        }

$jsonInhalt.Allgemein.PfadCfg    = "C:\temp\Config\Config.json"  
$jsonInhalt.Allgemein.PfadLib     = "C:\temp\Lib\Lib.json"  
$jsonInhalt.Laufwerke.C = "C:\"  
$jsonInhalt.Laufwerke.D = "D:\"  
$jsonInhalt.Laufwerke.E = "E:\"  
$jsonInhalt.Services.Hersteller1.Core.Dienstname      = "Core"  
$jsonInhalt.Services.Hersteller1.Core.Status                 = "Running"  
$jsonInhalt.Services.Hersteller1.Runtime.Dienstname   = "pext"  
$jsonInhalt.Services.Hersteller1.Runtime.Status          = "Running"  
$jsonInhalt.Services.Hersteller2.ManagementAgent.Dienstname = "SMA"  
$jsonInhalt.Services.Hersteller2.ManagementAgent.Status     = "Running"  

$jsonInhalt | ConvertTo-Json

Ausgabe:
{
    "Allgemein":  {  
                      "PfadLib":  "C:\\temp\\Lib\\Lib.json",  
                      "PfadCfg":  "C:\\temp\\Config\\Config.json"  
                  },
    "Laufwerke":  {  
                      "C":  "C:\\",  
                      "D":  "D:\\",  
                      "E":  "E:\\"  
                  },
    "Services":  {  
                     "Hersteller1":  {  
                                    "Core":  "System.Collections.Specialized.Orde  
redDictionary",  
                                    "Runtime":  "System.Collections.Specialized.O  
rderedDictionary"  
                                },
                     "Hersteller2":  {  
                                     "ManagementAgent":  "System.Collections.Specialized  
.OrderedDictionary"  
                                 }
                 }
}

Warum schreibt er mir in die Ausgabe "System.Collections.Specialized"?

Ich hätte folgendes erwartet:

{
    "Allgemein":  {  
                      "PfadLib":  "C:\\temp\\Lib\\Lib.json",  
                      "PfadCfg":  "C:\\temp\\Config\\Config.json"  
                  },
    "Laufwerke":  {  
                      "C":  "C:\\",  
                      "D":  "D:\\",  
                      "E":  "E:\\"  
                  },
    "Services":  {  
                     "Hersteller1":  {  
                                    "Core":  "System.Collections.Specialized.Orde  
redDictionary",  
                                    "Runtime":  "System.Collections.Specialized.O  
rderedDictionary"  
                                },
                     "Hersteller2":  {  
                                     "ManagementAgent":  {  
                                                                         "Dienstname" ="SMA",  
                                                                          "Status"="Running"  
                                      }
                                }
                 }
}

Ist doch das gleiche wie bei Laufwerke, nur 2 Ebenen mehr verschachtelt?
Oder wird mein grauer Star blind? =)

VG

Content-Key: 629291

Url: https://administrator.de/contentid/629291

Printed on: April 26, 2024 at 14:04 o'clock

Mitglied: 146707
Solution 146707 Dec 07, 2020 updated at 16:05:15 (UTC)
Goto Top
Tipp ...Parameter -Depth bei ConvertTo-Json face-wink.
-Depth
Specifies how many levels of contained objects are included in the JSON representation. The default value is 2.
Member: H41mSh1C0R
H41mSh1C0R Dec 07, 2020 updated at 16:07:01 (UTC)
Goto Top
Danköö

*grübel* warum man sowas überhaupt angeben muss, das ergibt sich doch aus der Struktur oder gibt es einen praktischen Grund das so zu tun.

OK Default 2 *gg*
Mitglied: 146707
146707 Dec 07, 2020 updated at 16:13:40 (UTC)
Goto Top
Zitat von @H41mSh1C0R:
*grübel* warum man sowas überhaupt angeben muss, das ergibt sich doch aus der Struktur oder gibt es einen praktischen Grund das so zu tun.
Ja war eine schlechte Wahl seitens MS, aber in der neuen PS Versionen wurde das endlich geändert
https://powershell.anovelidea.org/powershell/ps7now-changes-to-json-cmdl ...
In PowerShell 7, Get-Help -Name ConvertFrom-Json -Full reveals that -Depth parameter accepts type [Int32] and has a default value of 1024. This is already a great improvement over the older cmdlet.