Powershell创建数组数组 [英] Powershell create array of arrays

查看:103
本文介绍了Powershell创建数组数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Powershell将数据推送到REST api.

I'm trying to push data to a REST api using powershell.

http://influxdb.com/docs/v0.8/api/reading_and_writing_data.html

服务器希望像这样的数据:

The server expects data like so:

[
  {
    "name" : "hd_used",
    "columns" : ["value", "host", "mount"],
    "points" : [
      [23.2, "serverA", "mnt"]
    ]
  }
]

但是,我只能制作一个看起来像这样的json对象(注意多余的引号):

However, I"m only able to make a json object that looks like this (notice the extra quotes):

[
  {
    "name" : "hd_used",
    "columns" : ["value", "host", "mount"],
    "points" : [
      "[23.2, "serverA", "mnt"]"
    ]
  }
]

如何在不将嵌套数组用引号引起来的情况下将数据构造到数组数组中?

How can I construct the data into an array of arrays without wrapping the nested array in quotes?

这有效,但它不是嵌套数组

This works, but it isn't a nested array

$influxdata = [ordered]@{}
$influxdata.name = $hd_used
$influxdata.columns = @("value", "host", "mount")
$influxdata.points = @()
$influxdata.points += @("23.2", "serverA", "mnt")
$influxdatajson = $influxdata | ConvertTo-Json -Depth 2

这有效,但是内部数组实际上是一个字符串.

This works but the inner array is actually a string.

$influxdata = [ordered]@{}
$influxdata.name = $hd_used
$influxdata.columns = @("value", "host", "mount")
$influxdata.points = @()
$influxdata.points += @('["23.2", "serverA", "mnt"]')
$influxdatajson = $influxdata | ConvertTo-Json -Depth 2

推荐答案

$PSVersion.$PSVersion等于3.0且您的 exact 输入下,当打印$influxdatajson变量时,我得到以下信息:

With $PSVersion.$PSVersion equal to 3.0 and your exact input I get the following when I print the $influxdatajson variable:

{
    "name":  "hd_used",
    "columns":  [
                    "value",
                    "host",
                    "mount"
                ],
    "points":  [
                   "23.2",
                   "serverA",
                   "mnt"
               ]
}

显然不是您想要的,但不是您说的得到的.

Which clearly isn't what you want but isn't what you said you got either.

我们得到的输出的原因是因为您尝试将阵列添加到现有阵列中的方式没有按您期望的方式工作,因为powershell讨厌展开阵列(我认为).

The reason that that is the output we get is because your attempt to add the array to the existing array didn't work the way you expect because of powershell's annoying tendency to unroll arrays (I think).

如果您改用以下语法来解决这种奇怪情况:

If you work around that oddity by using this syntax instead:

$influxdata.points += ,@("23.2", "serverA", "mnt")

(前导,强制使用数组上下文,以便展开外部数组而不是您尝试添加的数组)

(the leading , forces an array context so that outer array gets unrolled instead of the array you are trying to add)

然后我从$influxdatajson获得以下输出:

then I get the following output from $influxdatajson:

{
    "name":  "hd_used",
    "columns":  [
                    "value",
                    "host",
                    "mount"
                ],
    "points":  [
                   [
                       "23.2",
                       "serverA",
                       "mnt"
                   ]
               ]
}

这篇关于Powershell创建数组数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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