我怎么能转换hastable在PowerShell中一个JSON字符串? [英] How can I convert a hastable to a json string in powershell?

查看:237
本文介绍了我怎么能转换hastable在PowerShell中一个JSON字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个哈希表转换为一个JSON对象用于Web服务使用PowerShell 2.0。

I'm trying to convert a hashtable to a json object for use in a web service with powershell 2.0.

$testhash = @{
    Name = 'John Doe'
    Age = 10
    Amount = 10.1
    MixedItems = (1,2,3,"a")
    NestedHash = @{
        nestedkey = "nextedvalue"
    }
}

function toJson($obj){

    $ms = New-Object IO.MemoryStream
    $type = $obj.getType()
    [Type[]]$types = ($obj | select -expand PsTypeNames |  Select -unique) + [type]'System.Management.Automation.PSObject'
    $js = New-Object System.Runtime.Serialization.Json.DataContractJsonSerializer $type, $types, ([int]::MaxValue), $false, $null, $false
    $js.writeObject($ms, $obj) | out-null
    $utf8.GetString( $ms.ToArray(), 0, $ms.Length )
    $ms.Dispose() | out-null
}

toJson $testhash
'[{"Key":"Name","Value":"John Doe"},{"Key":"Age","Value":10},{"Key":"Amount","Value":10.1},{"Key":"NestedHash","Value":[{"__type":"KeyValuePairOfanyTypeanyType:#System.Collections.Generic","key":"nestedkey","value":"nextedvalue"}]},{"Key":"MixedItems","Value":[1,2,3,"a"]}]'

我使用 DataContractJsonSerializer构造中,应该晚饭的方式preSS型信息,但它显然不是。我也逗乐它的提取键和值对,但我想拥有它没有做,要么。我究竟做错了什么?

I'm using DataContractJsonSerializer constructor in a way that should suppress type information but it's obviously not. I'm also amused by it's extracting key and value pairs but I'd like to have it not do that either. What am I doing wrong?

推荐答案

我在这里改编自脚本如下:

$testhash = @{
    Name = 'John Doe'
    Age = 10
    Amount = 10.1
    MixedItems = (1,2,3,"a")
    NestedHash = @{
        nestedkey = "nextedvalue"
    }
}

function Read-Stream {
PARAM(
   [Parameter(Position=0,ValueFromPipeline=$true)]$Stream
)
process {
   $bytes = $Stream.ToArray()
   [System.Text.Encoding]::UTF8.GetString($bytes,0,$bytes.Length)
}}

function New-Json {
[CmdletBinding()]
param([Parameter(ValueFromPipeline=$true)][HashTable]$InputObject) 
begin { 
   $ser = @{}
   $jsona = @()
}
process {
   $jsoni = 
   foreach($input in $InputObject.GetEnumerator() | Where { $_.Value } ) {
      if($input.Value -is [Hashtable]) {
         '"'+$input.Key+'": ' + (New-JSon $input.Value)
      } else {
         $type = $input.Value.GetType()
         if(!$Ser.ContainsKey($Type)) {
            $Ser.($Type) = New-Object System.Runtime.Serialization.Json.DataContractJsonSerializer $type
         }
         $stream = New-Object System.IO.MemoryStream
         $Ser.($Type).WriteObject( $stream, $Input.Value )
         '"'+$input.Key+'": ' + (Read-Stream $stream)
      }
   }

   $jsona += "{`n" +($jsoni -join ",`n")+ "`n}"
}
end { 
   if($jsona.Count -gt 1) {
      "[$($jsona -join ",`n")]" 
   } else {
      $jsona
   }
}}

$testHash | New-Json

这篇关于我怎么能转换hastable在PowerShell中一个JSON字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆