我已经判断过Foreach和Foreach-Object,但即使使用ConvertFrom-json也无法迭代出任何值,所以我对这一部分有点摸不着头脑--知道如何迭代它的每个单元格吗?

[
    [
        [
            "test-1234",
            "2023-07-11T04:00:24+00:00"
        ]
    ],
    [
        [
            "sb-00091",
            "2023-07-11T04:00:21+00:00"
        ]
    ]
]

以及如何从下面的集合中迭代和获取每个值

[
    [
        [
            "test-999",
            "2023-07-13T04:00:21+00:00",
            [
                [
                    "checking"
                ]
            ]
        ]
    ]
]

如有任何帮助,将不胜感激

one more point is if I would like to iterate through and would like to get the corresponding value of Timestamp i.e. for test-1234 it is 2023-07-11T04:00:24+00:00 , then how should I need to update the loop

a                            b
test-1234                  2023-07-11T04:00:24+00:00
sb-00091                   2023-07-11T04:00:21+00:00

推荐答案

Json文件中包含的是嵌套数组而不是单元格,您可以使用Queue<T>来遍历嵌套数组,直到达到标量值.同样的逻辑可以用于您问题中提供的两个JSON文件.This answer演示了可用于flatten个深度未知的嵌套数组的不同替代方案.

$json = Get-Content path\to\json.json -Raw | ConvertFrom-Json
$queue = [System.Collections.Generic.Queue[object]]::new()
$queue.Enqueue($json)

while ($queue.Count) {
    $items = $queue.Dequeue()
    foreach ($item in $items) {
        if ($item -is [System.Collections.ICollection]) {
            $queue.Enqueue($item)
            continue
        }
        "Item: $item"
    }
}

第一个Json输出:

Item: test-1234
Item: 07/11/2023 01:00:24
Item: sb-00091
Item: 07/11/2023 01:00:21

第二个Json输出:

Item: test-999
Item: 07/13/2023 01:00:21
Item: checking

Json相关问答推荐

2020-12年草案中的Json列举模式

Google Page to JSON应用程序脚本出现CORS错误

在MongoDB中检索某个月份和年份的JSON文档

错误:在 NX 工作区中找不到模块../swagger.json

APIM 生成 JsonArray 到 EventHub

使用 JQ 获取 JSON 中的替代元素(输出:JSON 对象)

将 colly 包输出文本添加到 golang 中的映射

从字节解码 JSON 数据,将 float 值更改为 int

Vue 3如何将参数作为json发送到axios get

C# 合并 2 个几乎相同的 JSON 对象

JQuery,使用 GET 方法发送 JSON 对象

JSON RPC - 什么是id?

JSON extract\set 的 SQLite JSON1 示例

Select 什么数据类型json或者jsonb或者text

我们可以使用 JSON 作为数据库吗?

如何创建 JSON 对象 Node.js

将循环 struct 转换为 JSON - 有什么方法可以找到它抱怨的字段?

如何向 json IAM 策略添加 comments ?

NSURLRequest 中不支持的 URL

JSON.stringify 向我的 Json 对象添加额外的 \ 和 "" 的问题