简体   繁体   English

当选择失败时,为什么foreach在这里工作?

[英]Why does foreach work here when select fails?

I've stored a list of attachments from my RavenDB server in the $Response variable. 我在$Response变量中存储了RavenDB服务器中的附件列表。

[{"Size":3040,"Key":"attachments/deployments-7928/output-log","Metadata":{"ContentType":"text/xml"},"Etag":"02000000-0000-0014-0000-00000000A0D0"},{"Size":2524,"Key":"attachments/deployments-7927/output-log","Metadata":{
"ContentType":"text/xml"},"Etag":"02000000-0000-0014-0000-00000000A0CF"},{"Size":530,"Key":"attachments/tasks-7842/output-log","Metadata":{"ContentType":"text/xml"},"Etag":"02000000-0000-0014-0000-00000000A0AA"}]

I want to get the Key value so I can fetch the attachments. 我想获取Key值,以便我可以获取附件。

Why can't I select the Key value like this? 为什么我不能像这样选择Key值?

$Response | ConvertFrom-Json | Select Key

All the keys look empty when I do that. 当我这样做时,所有的键看起来都是空的。

Bizarrely, it works when I use Foreach instead. 奇怪的是,它在我使用Foreach时起作用。

$Response | ConvertFrom-Json | Foreach { $_.Key }

I see this: 我看到了这个:

attachments/deployments-7928/output-log
attachments/deployments-7927/output-log
attachments/tasks-7842/output-log

What's the difference? 有什么不同?

Why doesn't Select work here? 为什么不在这里选择工作?

It works if you assign the results to a variable first, and then pass that into Select-Object . 如果首先将结果分配给变量,然后将其传递给Select-Object ,则它可以工作。

$response = @'
    [{"Size":3040,"Key":"attachments/deployments-7928/output-log","Metadata":{"ContentType":"text/xml"},"Etag":"02000000-0000-0014-0000-00000000A0D0"},{"Size":2524,"Key":"attachments/deployments-7927/output-log","Metadata":{
    "ContentType":"text/xml"},"Etag":"02000000-0000-0014-0000-00000000A0CF"},{"Size":530,"Key":"attachments/tasks-7842/output-log","Metadata":{"ContentType":"text/xml"},"Etag":"02000000-0000-0014-0000-00000000A0AA"}]
'@

$ObjectList = $response | ConvertFrom-Json;

$ObjectList | Select-Object -Property Key;

Because convertfrom-json does not return array of PSObjects, it returns an array system.object[]. 因为convertfrom-json不返回PSObjects数组,所以它返回一个数组system.object []。 The array does not have a 'Key' member. 该数组没有“Key”成员。

 Write-Output '[{"Size":3040,"Key":"attachments/deployments-7928/output-log","Metadata":{"ContentType":"text/xml"},"Etag":"02000000-0000-0014-0000-00000000A0D0"},{"Size":2524,"Key":"attachments/deployments-7927/output-log","Metadata":{
"ContentType":"text/xml"},"Etag":"02000000-0000-0014-0000-00000000A0CF"},{"Size":530,"Key":"attachments/tasks-7842/output-log","Metadata":{"ContentType":"text/xml"},"Etag":"02000000-0000-0014-0000-00000000A0AA"}]' `
| convertfrom-json | get-member

Output 产量

   TypeName: System.Object[]
   ...

See, it only has the following members : 看,它只有以下成员:

Name 名称

Count 计数
Add
Address 地址
Clear 明确
Clone 克隆
CompareTo 相比于
Contains 包含
CopyTo 复制到
Equals 等于
Get 得到
GetEnumerator 的GetEnumerator
GetHashCode GetHashCode的
GetLength 对GetLength
GetLongLength GetLongLength
GetLowerBound GetLowerBound
GetType 的GetType
GetUpperBound GetUpperBound
GetValue 的GetValue
IndexOf 指数
Initialize 初始化
Insert 插入
Remove 去掉
RemoveAt RemoveAt移除
Set
SetValue 设定值
ToString 的ToString
Item 项目
IsFixedSize IsFixedSize
IsReadOnly IsReadOnly
IsSynchronized IsSynchronized
Length 长度
LongLength LongLength
Rank
SyncRoot SyncRoot上

Assigning to a variable converts to TypeName: System.Management.Automation.PSCustomObject and this has a 'key' member. 分配给变量会转换为TypeName: System.Management.Automation.PSCustomObject并且它具有“键”成员。 Powershell's built-in enumeration awareness expands this properly. Powershell的内置枚举意识正确地扩展了这一点。

如果要避免中间变量赋值:

{$response | ConvertFrom-Json}.invokereturnasis() | select key

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

相关问题 当我单击 select 选项时,我的警报不起作用 - My alert does not work when I click on select option 为什么jQuery ajax(JSONP)在没有`&callback =`的情况下工作,但是在`&callback = functionname`的情况下失败? - Why does jQuery ajax (JSONP) work without `&callback=` but fails with `&callback=functionname` Gson deearilize list 和 class,擦除在这里如何工作? - Gson desearilize list and class, how does erasure work here? 为什么fetch()不起作用? - Why fetch() does not work? 为什么Firefox在这里引发JSON解析错误? - Why does Firefox throw on JSON parse error here? 为什么我的浏览器在这里说“未捕获的参考错误”? - Why does my browser say “uncaught referenceerror” here? JSONObject为什么getString在这里没有得到任何值? - JSONObject why does getString not getting any value here? 为什么目录正确时file_get_content无法找到文件? - Why does file_get_content fails to locate file when the directory is correct? 为什么 Laravels assertJson 在使用必须检查根属性时会失败? - Why does Laravels assertJson fails when using has to check in root properties? 在asp.net MVC中使用json时,Foreach不起作用 - Foreach does not work while using json in asp.net mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM