意外的ConvertTo-Json结果?答案:它的默认深度为2 [英] Unexpected ConvertTo-Json results? Answer: it has a default -Depth of 2

查看:120
本文介绍了意外的ConvertTo-Json结果?答案:它的默认深度为2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么会出现意外 ConvertTo-Json 结果?
为什么往返($Json | ConvertFrom-Json | ConvertTo-Json)失败?

Why do I get unexpected ConvertTo-Json results?
And why does a round-trip ($Json | ConvertFrom-Json | ConvertTo-Json) fail?

Stackoverflow有一个很好的机制来防止重复的问题,但据我所知,还没有一种机制可以防止具有重复原因的问题.以这个问题为例:几乎每个星期都有一个新问题以相同的原因出现,但是通常很难将其定义为重复问题,因为问题本身只是稍有不同. 尽管如此,如果这个问题/答案本身以重复(或脱题)结尾,我也不会感到惊讶,但是不幸的是stackoverflow不可能

Stackoverflow has a good mechanism to prevent duplicate questions but as far as I can see there is no mechanism to prevent questions that have a duplicate cause. Take this question as a an example: almost every week a new question comes in with the same cause, yet it is often difficult to define it as a duplicate because the question itself is just a slightly different. Nevertheless, I wouldn't be surprised if this question/answer itself ends up as a duplicate (or off-topic) but unfortunately stackoverflow has no possibility to write an article to prevent other programmers from continuing writing questions caused by this "known" pitfall.

一些具有相同常见原因的类似问题的示例:

A few examples of similar questions with the same common cause:

  • PowerShell ConvertTo-Json does not convert Array as expected (yesterday)
  • Powershell ConvertTo-json with embedded hashtable
  • powershell "ConvertTo-Json" has messed json format output
  • Nested arrays and ConvertTo-Json
  • Powershell ConvertTo-JSON missing nested level
  • How to save a JSON object to a file using Powershell?
  • Cannot convert PSCustomObjects within array back to JSON correctly
  • ConvertTo-Json flattens arrays over 3 levels deep
  • Add an array of objects to a PSObject at once
  • Why does ConvertTo-Json drop values
  • How to round-trip this JSON to PSObject and back in Powershell

那么,这个自我回答"的问题与上述重复的问题有什么不同吗?
标题中有共同原因,因此可以更好地防止由于相同原因而重复问题.

So, were does this "self-answered" question differ from the above duplicates?
It has the common cause in the title and with that it might better prevent repeating questions due to the same cause.

推荐答案

答案

ConvertTo-Json 具有 -Depth 参数:

Answer

ConvertTo-Json has a -Depth parameter:

指定包含在其中的对象的级别数 JSON表示形式.
默认值为 2 .

Specifies how many levels of contained objects are included in the JSON representation.
The default value is 2.

示例

要使用JSON文件进行完整往返,您需要增加ConvertTo-Json cmdlet的-Depth:

Example

To do a full round-trip with a JSON file you need to increase the -Depth for the ConvertTo-Json cmdlet:

$Json | ConvertFrom-Json | ConvertTo-Json -Depth 9

TL; DR

可能是因为ConvertTo-Json使用全名(.Net)终止了比默认-Depth( 2 )更深的分支,程序员假定存在错误或cmdlet限制,并且没有阅读帮助或关于.
就我个人而言,我认为该字符串的末尾带有一个简单的
省略号(三个点:…).切断的分支将具有更清晰的含义(另请参见:Github问题: 8381 )

TL;DR

Probably because ConvertTo-Json terminates branches that are deeper than the default -Depth (2) with a (.Net) full type name, programmers assume a bug or a cmdlet limitation and do not read the help or about.
Personally, I think a string with a simple ellipsis (three dots: …) at the end of the cut off branch, would have a clearer meaning (see also: Github issue: 8381)

这个问题通常还会在另一个讨论中结束: 为什么深度是完全受限制的?

This issue often ends up in another discussion as well: Why is the depth limited at all?

某些对象具有循环引用,这意味着子对象可以引用父项(或其祖父母之一),如果将其序列化为JSON则会导致无限循环.

Some objects have circular references, meaning that a child object could refer to a parent (or one of its grandparents) causing a infinitive loop if it would be serialized to JSON.

例如,下面的哈希表具有引用对象本身的parent属性:

Take for example the following hash table with a parent property that refers to the object itself:

$Test = @{Guid = New-Guid}
$Test.Parent = $Test

如果执行:$Test | ConvertTo-Json,默认情况下它将方便地停在2深度处:

If you execute: $Test | ConvertTo-Json it will conveniently stop at a depth level of 2 by default:

{
    "Guid":  "a274d017-5188-4d91-b960-023c06159dcc",
    "Parent":  {
                   "Guid":  "a274d017-5188-4d91-b960-023c06159dcc",
                   "Parent":  {
                                  "Guid":  "a274d017-5188-4d91-b960-023c06159dcc",
                                  "Parent":  "System.Collections.Hashtable"
                              }
               }
}

这就是为什么自动将-Depth设置为大量不是一个好主意的原因.

This is why it is not a good idea to automatically set the -Depth to a large amount.

这篇关于意外的ConvertTo-Json结果?答案:它的默认深度为2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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