我试图使用PowerShell将数据推送到REST API。
http://influxdb.com/docs/v0.8/api/reading_and_writing_data.html
服务器期望如此的数据:
[ { "name" : "hd_used", "columns" : ["value", "host", "mount"], "points" : [ [23.2, "serverA", "mnt"] ] } ]但是,我只能做一个看起来像这样的JSON对象(注意额外的引号):
[ { "name" : "hd_used", "columns" : ["value", "host", "mount"], "points" : [ "[23.2, "serverA", "mnt"]" ] } ]我怎样才能将数据构造成一个数组的数组而不用引号包裹嵌套数组?
这工作,但它不是一个嵌套的数组
$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这工作,但内部数组实际上是一个字符串。
$influxdata = [ordered]@{} $influxdata.name = $hd_used $influxdata.columns = @("value", "host", "mount") $influxdata.points = @() $influxdata.points += @('["23.2", "serverA", "mnt"]') $influxdatajson = $influxdata | ConvertTo-Json -Depth 2I'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"] ] } ]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 2This 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 ,您的确切输入当我打印$influxdatajson变量时,我得到以下内容:
{ "name": "hd_used", "columns": [ "value", "host", "mount" ], "points": [ "23.2", "serverA", "mnt" ] }这显然不是你想要的,但不是你说的你得到的。
这是我们得到的输出的原因是因为尝试将数组添加到现有数组并不符合您的期望,因为powershell展开数组(我认为)很烦人。
如果你用这个语法来解决这个问题:
$influxdata.points += ,@("23.2", "serverA", "mnt")(前导,强制数组上下文,以便外数组展开,而不是你试图添加的数组)
然后我从$influxdatajson获得以下输出:
{ "name": "hd_used", "columns": [ "value", "host", "mount" ], "points": [ [ "23.2", "serverA", "mnt" ] ] }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.
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)
then I get the following output from $influxdatajson:
{ "name": "hd_used", "columns": [ "value", "host", "mount" ], "points": [ [ "23.2", "serverA", "mnt" ] ] }Powershell创建数组的数组(Powershell create array of arrays)我试图使用PowerShell将数据推送到REST API。
http://influxdb.com/docs/v0.8/api/reading_and_writing_data.html
服务器期望如此的数据:
[ { "name" : "hd_used", "columns" : ["value", "host", "mount"], "points" : [ [23.2, "serverA", "mnt"] ] } ]但是,我只能做一个看起来像这样的JSON对象(注意额外的引号):
[ { "name" : "hd_used", "columns" : ["value", "host", "mount"], "points" : [ "[23.2, "serverA", "mnt"]" ] } ]我怎样才能将数据构造成一个数组的数组而不用引号包裹嵌套数组?
这工作,但它不是一个嵌套的数组
$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这工作,但内部数组实际上是一个字符串。
$influxdata = [ordered]@{} $influxdata.name = $hd_used $influxdata.columns = @("value", "host", "mount") $influxdata.points = @() $influxdata.points += @('["23.2", "serverA", "mnt"]') $influxdatajson = $influxdata | ConvertTo-Json -Depth 2I'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"] ] } ]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 2This 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 ,您的确切输入当我打印$influxdatajson变量时,我得到以下内容:
{ "name": "hd_used", "columns": [ "value", "host", "mount" ], "points": [ "23.2", "serverA", "mnt" ] }这显然不是你想要的,但不是你说的你得到的。
这是我们得到的输出的原因是因为尝试将数组添加到现有数组并不符合您的期望,因为powershell展开数组(我认为)很烦人。
如果你用这个语法来解决这个问题:
$influxdata.points += ,@("23.2", "serverA", "mnt")(前导,强制数组上下文,以便外数组展开,而不是你试图添加的数组)
然后我从$influxdatajson获得以下输出:
{ "name": "hd_used", "columns": [ "value", "host", "mount" ], "points": [ [ "23.2", "serverA", "mnt" ] ] }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.
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)
then I get the following output from $influxdatajson:
{ "name": "hd_used", "columns": [ "value", "host", "mount" ], "points": [ [ "23.2", "serverA", "mnt" ] ] }
发布评论