gpt4 book ai didi

linux - 循环 json 文件/数组 linux bash

转载 作者:太空宇宙 更新时间:2023-11-04 10:08:06 26 4
gpt4 key购买 nike

我有一个如下所示的 json 文件:

{"data":{"players":[{"id":"aaaa","is_multiple":false,"name":"TV Woonkamer","password_protected":false,"support_seek":false,"support_set_volume":false,"type":"upnp"},{"id":"bbbb","is_multiple":false,"name":"squeezelite","password_protected":false,"support_seek":true,"support_set_volume":true,"type":"upnp"},{"id":"cccc","is_multiple":false,"name":"Woonkamer","password_protected":false,"support_seek":true,"support_set_volume":true,"type":"airplay"},{"id":"__dddd__","is_multiple":true,"name":"Multiple AirPlay Devices","password_protected":false,"support_seek":true,"support_set_volume":true,"type":"airplay"}]},"success":true}

由 PHP 解析:

stdClass Object
(
[data] => stdClass Object
(
[players] => Array
(
[0] => stdClass Object
(
[id] => uuid:aaaa
[is_multiple] =>
[name] => TV Woonkamer
[password_protected] =>
[support_seek] =>
[support_set_volume] =>
[type] => upnp
)

[1] => stdClass Object
(
[id] => uuid:bbbb
[is_multiple] =>
[name] => squeezelite
[password_protected] =>
[support_seek] => 1
[support_set_volume] => 1
[type] => upnp
)

[2] => stdClass Object
(
[id] => cccc
[is_multiple] =>
[name] => Woonkamer
[password_protected] =>
[support_seek] => 1
[support_set_volume] => 1
[type] => airplay
)

[3] => stdClass Object
(
[id] => __dddd__
[is_multiple] => 1
[name] => Multiple AirPlay Devices
[password_protected] =>
[support_seek] => 1
[support_set_volume] => 1
[type] => airplay
)

)

)

[success] => 1
)

我想从 linux 命令行循环遍历它,找到 is_multiple 为空且类型为 airplay 的所有播放器 ID。下面显示了所有类型为airplay的播放器列表,但是如何开始循环?

jq '.data .players[]' scan.json | jq 'select(.type=="airplay")' | jq -r '.id'

最佳答案

我相信你之后的代码是这样的

<scan.json jq -r '.data.players[]|select(.type == "airplay" and .is_multiple == false)|.id' |
xargs -I{} wget -q "https://my.web/?id={}"

如果对每个 id 的操作更复杂,您可以将其包装在 .sh 脚本中,或在 bash 函数中(如 process_one_id( ){ 命令.. ;}.

注意:要将 bash 函数与 xargs 一起使用,您必须执行 export -f process_one_id 然后通过 bash 调用它,如 xargs -I {} bash -c process_one_id {}。或者使用 GNU parallel 这使得调用导出的 shell 函数变得透明。

关于linux - 循环 json 文件/数组 linux bash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50903499/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com