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

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

问题描述

概述

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

TL;DR::

PS>$psobj = $orig |ConvertFrom-JSONPS>$psobj |转换为 JSON

... 给了我不同的结果:

PS>$orig |ConvertFrom-JSON |转换为 JSON

原始数据

<预><代码>[{"类型": "1",名称":质量保证"},{"类型": "2",名称":随便"}]

分两步进行转换

我将删除空格(使其适合一行...),将其转换为 powershell 对象,然后将其转换回 JSON.这很有效,并为我提供了正确的数据:

PS>$orig = '[{"Type": "1","Name": "QA"},{"Type": "2","Name": "DEV"}]'PS>$psobj = $orig |ConvertFrom-JSONPS>$psobj |转换为 JSON[{"类型": "1",名称":质量保证"},{"类型": "2",名称":开发"}]

用管道将两步结合起来

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

PS>$orig |ConvertFrom-JSON |转换为 JSON{价值":  [{"类型": "1",名称":质量保证"},{"类型": "2",名称":开发"}],计数":2}

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

解决方案

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

PS C:>($orig | ConvertFrom-JSON) |转换为 JSON[{"类型": "1",名称":质量保证"},{"类型": "2",名称":开发"}]

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

使用括号是对输出进行分组"的一种更简短的方式,允许您在不创建变量的情况下对其进行操作.

Overview

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: this:

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

... gives me different result than this:

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

Original data

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

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
}

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"
    }
]

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天全站免登陆