为什么在转换JSON时,Powershell在单行代码与两行代码中给出不同的结果? [英] Why does powershell give different result in one-liner than two-liner when converting JSON?

查看:85
本文介绍了为什么在转换JSON时,Powershell在单行代码与两行代码中给出不同的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在powershell 3提示符下,我想调用RESTful服务,获取一些JSON,然后漂亮地打印它.我发现,如果将数据转换为powershell对象,然后再将powershell对象转换回json,我会得到一个漂亮的漂亮打印字符串.但是,如果我将这两个转换与一个管道组合成一个单一的管道,我会得到不同的结果.

From a powershell 3 prompt,, I want to call a RESTful service, get some JSON, and pretty-print it. I discovered that if I convert the data to a powershell object, and then convert the powershell object back to json, I get back a nice pretty-printed string. However, if I combine the two conversions into a one-liner with a pipe I will get a different result.

TL; DR:此:

PS> $psobj = $orig | ConvertFrom-JSON
PS> $psobj | ConvertTo-JSON

...给我的结果与此不同:

... gives me different result than this:

PS> $orig | ConvertFrom-JSON | ConvertTo-JSON

原始数据

[
  {
    "Type": "1",
    "Name": "QA"
  },
  {
    "Type": "2",
    "Name": "whatver"
  }
]

分两步进行转换

我将删除空格(因此它只适合一行...),将其转换为powershell对象,然后将其转换回JSON.这样效果很好,并给了我正确的数据:

Doing the conversion in two steps

I'm going to remove the whitespace (so it fits on one line...), convert it to a powershell object, and then convert it back to JSON. This works well, and gives me back the correct data:

PS> $orig = '[{"Type": "1","Name": "QA"},{"Type": "2","Name": "DEV"}]'
PS> $psobj = $orig | ConvertFrom-JSON
PS> $psobj | ConvertTo-JSON
[
    {
        "Type":  "1",
        "Name":  "QA"
    },
    {
        "Type":  "2",
        "Name":  "DEV"
    }
]

将两个步骤与管道结合起来

但是,如果我将最后两个语句组合成一条直线,则会得到不同的结果:

Combining the two steps with a pipe

However, if I combine those last two statements into a one-liner, I get a different result:

PS> $orig | ConvertFrom-JSON | ConvertTo-JSON
{
    "value":  [
                  {
                      "Type":  "1",
                      "Name":  "QA"
                  },
                  {
                      "Type":  "2",
                      "Name":  "DEV"
                  }
              ],
    "Count":  2
}

请注意添加了键值"和计数".为什么有区别?我确定它与返回JSON对象而不是JSON数组的愿望有关,但是我不明白为什么我进行转换的方式会影响最终结果.

Notice the addition of the keys "value" and "Count". Why is there a difference? I'm sure it has something to do with the desire to return JSON object rather than a JSON array, but I don't understand why the way I do the conversion affects the end result.

推荐答案

解决方案是用括号将前两个操作包装起来:

The solution is to wrap the first two operations with parenthesis:

PS C:\> ($orig | ConvertFrom-JSON) | ConvertTo-JSON
[
    {
        "Type":  "1",
        "Name":  "QA"
    },
    {
        "Type":  "2",
        "Name":  "DEV"
    }
]

通过括号可以同时获取前两个操作的输出.没有它们,powershell将尝试解析任何单独获取的对象.由$orig | ConvertFrom-JSON生成的PSCustomObject集合包含两个PSCustomObjects用于1/QA和2/DEV对,因此通过管道传递该集合的输出,powershell尝试一次处理键/值对.时间.

The parenthesis allow you to grab the output of the first two operations all at once. Without them, powershell will attempt to parse any objects its gets separately. The collection of PSCustomObject resulting from $orig | ConvertFrom-JSON contains two PSCustomObjects for the 1/QA and 2/DEV pairs, so by piping the output of that collection powershell attempts to handle the key/value pairs one-at-a-time.

使用括号是对输出进行分组"的一种较短方法,它使您可以在不进行变量的情况下对其进行操作.

Using parenthesis is a shorter way of "grouping" that output and allows you to operate on it without making a variable.

这篇关于为什么在转换JSON时,Powershell在单行代码与两行代码中给出不同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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