Hashtables from ConvertFrom-json have different type from powershells built-in hashtables, how do I make them the same?

前端 未结 5 1300
天命终不由人
天命终不由人 2020-12-09 10:33

I have a json file (test.json) that looks something like this:

{
    \"root\":
    {
        \"key\":\"value\"
    }
}

I\'m loading it into

5条回答
  •  我在风中等你
    2020-12-09 10:59

    I put this together to handle nested json to hashtables

        function ConvertJSONToHash{
        param(
            $root
        )
        $hash = @{}
    
        $keys = $root | gm -MemberType NoteProperty | select -exp Name
    
        $keys | %{
            $key=$_
            $obj=$root.$($_)
            if($obj -match "@{")
            {
                $nesthash=ConvertJSONToHash $obj
                $hash.add($key,$nesthash)
            }
            else
            {
               $hash.add($key,$obj)
            }
    
        }
        return $hash
    }
    

    I have only tested with 4 levels but recursive until it has complete hashtable.

提交回复
热议问题