gpt4 book ai didi

json - 如何过滤嵌套的 SwiftyJSON 数组

转载 作者:行者123 更新时间:2023-11-28 16:07:49 25 4
gpt4 key购买 nike

我有一个嵌套多个级别的 SwiftyJSON 数组,我需要根据最低级别的值过滤该数组。下面是一个数组的例子。我需要在 Active == true 上过滤它。实现这一目标的最有效方法是什么?

var colors = JSON([
"Apple" : [
"Yellow" : [
"Active" : true,
"Working" : true,
"Value" : 0
],
"Red" : [
"Active" : false,
"Working" : true,
"Value" : 0
]
],
"Banana" : [
"Blue" : [
"Active" : false,
"Working" : true,
"Value" : 0
],
"Green" : [
"Active" : true,
"Working" : true,
"Value" : 0
]
]
])

期望的输出:

"Apple" : [
"Yellow" : [
"Active" : true,
"Working" : true,
"Value" : 0
]
],
"Banana" : [
"Green" : [
"Active" : true,
"Working" : true,
"Value" : 0
]
]

最佳答案

检查一下

var filteredArray = [JSON]()

for item in colors {
for subItem in item.1 {
if subItem.1["Active"].boolValue {
filteredArray.append(JSON([item.0:JSON([subItem.0:subItem.1])]))
}
}
}

print(filteredArray)

输出是包含 Active true 的已过滤子词典的 Dictionaries 数组:

[{
"Apple" : {
"Yellow" : {
"Working" : true,
"Value" : 0,
"Active" : true
}
}
}, {
"Banana" : {
"Green" : {
"Working" : true,
"Value" : 0,
"Active" : true
}
}
}]

关于json - 如何过滤嵌套的 SwiftyJSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39985964/

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