带有javascriptSerializer的powershell 2.0中的Json循环引用

标签 json powershell powershell-2.0 javascriptserializer

我正在用 powershell 2.0 编写脚本,现在无法升级到 3.0 或更高版本。在此脚本中,我尝试使用此链接 ( PowerShell 2.0 ConvertFrom-Json and ConvertTo-Json implementation ) 中的代码将一些数据序列化为 JSON:

function ConvertTo-Json20([object] $item){
    add-type -assembly system.web.extensions
    $ps_js=new-object system.web.script.serialization.javascriptSerializer
    return $ps_js.Serialize($item)
}

我的问题是我以某种方式得到了一个循环引用,我真的不知道为什么。我设置了一小段测试数据,结构在powershell中看起来像这样:
$testRoot = @{
    "id" = "1"
    "children" = @( 
        @{
            "id" = "2"
            "children" = @( 
                @{
                    "id" = "2";
                };
                @{
                    "id" = "3";
                }
            );
        };
        @{
            "id" = "4"
            "children" = @( 
                @{
                    "id" = "5";
                }
            );
        }
    )
}

我知道它看起来很垃圾,但我只需要这种格式。

我需要序列化的结构有更多的层,所以更多的“ child ”并且有一点变得奇怪。

当我尝试这个时:
ConvertTo-Json20 $testRoot

一切正常。结构被解析如下:
{
   "id":"1",
   "children":[
        {
            "id":"2",
            "children":[
               {
                   "id":"2"
               },
               {
                   "id":"3"
               }
            ]
        },
        {
            "id":"4",
            "children":[
               {
                   "id":"5"
               }
            ]
        }
   ]
}

但现在问题来了。如前所述,该结构具有更多层,因此我尝试将其设置为数组中的数据。
ConvertTo-Json20 @($testRoot)

但它不起作用我只是收到一条错误消息:
Exception in method "Serialize" with 1 argument(s):  
"While serializing an object of type "System.Management.Automation.PSParameterizedProperty" a circular reference was discovered."
At C:\Users\a38732\Desktop\Temp.ps1:34 symbol:28
+     return $ps_js.Serialize <<<< ($item)
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

(我从德语翻译了错误信息,所以英文版本可能会有一些不同的词......)

最佳答案

一个问题是使用JavaScriptSerializer类本身。截至今天,documentation itself concedes it should not be used to serialize nor deserialize JSON .报价:

Json.NET should be used serialization and deserialization.



如果您能够使用第三方库,例如 Json.NET ,这是一个简单的函数,它根据 OP 中的数据结构执行您需要的操作:
function ConvertTo-JsonNet {
   [CmdletBinding()]
    param(
        [Parameter(Mandatory)] $object,
        [Parameter(Mandatory)] [string]$jsonNetPath,
        [switch]$indent,
        [switch]$preserveReferencesHandling
    )
    Add-Type -Path $jsonNetPath;

    $formatting = if ($indent.IsPresent) { [Newtonsoft.Json.Formatting]::Indented; }
    else { [Newtonsoft.Json.Formatting]::None; }

    $settings = New-Object Newtonsoft.Json.JsonSerializerSettings;
    if ($preserveReferencesHandling.IsPresent) { 
        $settings.PreserveReferencesHandling = [Newtonsoft.Json.PreserveReferencesHandling]::Objects;
    }

    [Newtonsoft.Json.JsonConvert]::SerializeObject($object, $formatting, $settings);
}

简单用法,假设 Newtonsoft.Json.dll与您的脚本位于同一目录中:
$dllPath = Join-Path $PSScriptRoot 'Newtonsoft.Json.dll';
ConvertTo-JsonNet @($testRoot) $dllPath;

输出:
[{"id":"1","children":[{"id":"2","children":[{"id":"2"},{"id":"3"}]},{"id":"4","children":[{"id":"5"}]}]}]

您可以从 nuget package project site 手动下载 .dll。 .它有一个 .nupkg文件扩展名,但它是一个压缩文件,因此将扩展名重命名为 .zip你准备好了。在 lib子目录中有 .dll 文件,适用于 2.0 到 4.5 的 .NET 版本。

关于带有javascriptSerializer的powershell 2.0中的Json循环引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39161783/

相关文章:

javascript - 点击时使用 Google Books API 获取 JSON 数据

java - 使用 Java 查找简单的 Active Directory 信息

linux - Windows powershell 中的 tail -f 是什么?

powershell - 在 powershell 脚本中添加超时部分

javascript - 如何从数组中获得唯一的结果

java - 无法将 JsonNull 转换为 JsonObject

c# - 使用 PowerShell 查找最接近的 System.Color 实例

windows - 如何在powershell中仅查看过去24小时内创建的文件

powershell - 使用 Powershell GUI 的实时数据

java - 如何将 JSON 数组存储在 JSON 数组结果中并显示在 ListView 中?