简体   繁体   English

使用相同引用嵌套的 powershell psobject。 来自 json

[英]powershell psobject nested with same reference. from json

I'm having difficulty accessing the nested part of a psobject.我无法访问 psobject 的嵌套部分。 I've used我用过

$response = Invoke-RestMethod

to pull back information from an application.从应用程序中提取信息。 $response then contains a json file. $response 然后包含一个 json 文件。

I've santized the json file but this is the general format.我已经清理了 json 文件,但这是通用格式。

Response:{
    "total":  2,
    "items":  [
                  {
                      "id":  1,
                      "name":  "127.0.0.1",
                      "properties":  [
                                               {
                                                   "name":  "var1",
                                                   "value":  "10"
                                               },
                                               {
                                                   "name":  "var2",
                                                   "value":  "20"
                                               },
                                               {
                                                   "name":  "var3",
                                                   "value":  "30"
                                               },
                                               {
                                                   "name":  "var4",
                                                   "value":  "40"
                                               }
                                            ]
                  },
                  {
                      "id":  2,
                      "name":  "10.2.2.2",
                      "properties":  [
                                               {
                                                   "name":  "var1",
                                                   "value":  "100"
                                               },
                                               {
                                                   "name":  "var2",
                                                   "value":  "200"
                                               },
                                               {
                                                   "name":  "var3",
                                                   "value":  "300"
                                               },
                                               {
                                                   "name":  "var4",
                                                   "value":  "400"
                                               },
                                               {
                                                   "name":  "var5",
                                                   "value":  "500"
                                               }
                                            ]
                  },
              ],
    "searchId":  null,
    "isMin":  false
}

I can easily access the id and name directly under the items layer using a simple for loop我可以使用简单的 for 循环直接在 items 层下轻松访问 id 和 name

for ( $index = 0; $index -lt $data.Items.count; $index++)
{
    $id=$data.Items.id[$index]
    $name=$data.Items.name[$index]
}

but i can't find out how to reference the stuff under the properties section.但我无法找到如何引用属性部分下的内容。

This is some of the stuff i've tried这是我尝试过的一些东西

    $var1 = Select-Object -ExpandProperty  $data.Items.properties[$index] 
    $var1 = $data.Items.properties[$index] | Select-Object -expand name
    $var1 = $data.Items.properties[$index] | Select -property *

Some of it gets close, but only returns one of the name entries and not all.其中一些接近,但只返回名称条目之一而不是全部。

It is important where you apply the index.在何处应用索引很重要。 Applying the index at the very end of your expression works in some cases, but not quite in the way, that you might expect.在某些情况下,在表达式的最后应用索引是可行的,但并不完全符合您的预期。 Lets have a closer look at what you are exactly doing:让我们仔细看看你到底在做什么:

$id = $data.Items.id[0]

$data.Items.id returns an array of all ids of all "Items" elements: $data.Items.id返回所有“Items”元素的所有 id 的数组:

PS> $data.Items.id

1
2

With $data.Items.id[0] you address the first element of that array, which is the value that you want.使用$data.Items.id[0]可以寻址该数组的第一个元素,即您想要的值。

Now compare that to the following:现在将其与以下内容进行比较:

$id = $data.Items[0].id

$data.Items returns an array of all "Items" elements: $data.Items返回所有“Items”元素的数组:

PS> $data.Items

id name      properties
-- ----      ----------
 1 127.0.0.1 {@{name=var1; value=10}, @{name=var2; value=20}, @{name=var3; value=30}, @{name=var4; value=40}}
 2 10.2.2.2  {@{name=var1; value=100}, @{name=var2; value=200}, @{name=var3; value=300}, @{name=var4; value=400....}

With $data.Items[0] you address the the first element of the array:使用$data.Items[0]处理数组的第一个元素:

PS> $data.Items[0]

id name      properties
-- ----      ----------
 1 127.0.0.1 {@{name=var1; value=10}, @{name=var2; value=20}, @{name=var3; value=30}, @{name=var4; value=40}}

And with $data.Items[0].id you address the the first element of the array and select only the content of it`s id-property.使用$data.Items[0].id处理数组的第一个元素并只选择它的 id-property 的内容。

The benefit of addressing a single object first before expanding its properties will be clear once you have properties, that contain more than a single value.一旦您拥有包含多个值的属性,在扩展其属性之前首先寻址单个对象的好处将很明显。 Compare the following output:比较以下输出:

PS> $data.Items.properties[0]

name value
---- -----
var1 10
PS> $data.Items[0].properties

name value
---- -----
var1 10
var2 20
var3 30
var4 40

As you said in the comments, you would further want to change the value of one particular property.正如您在评论中所说,您还想更改一个特定属性的值。 I suggest you narrow the properties down by using Where-Object :我建议您使用Where-Object缩小属性范围:

PS> $var2 = $data.Items[0].properties | Where-Object Name -eq 'var2'
PS> $var2.value = 25
PS> $data.Items[0].properties

name value
---- -----
var1 10
var2 25   <---
var3 30
var4 40 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM