ios - 在 SwiftyJson 中过滤数据

标签 ios json swift nspredicate swifty-json

我有一个 SwiftyJson 对象。

我无法过滤那个数组。我试过这个解决方案 https://stackoverflow.com/a/37497170/4831567 .但是我的 json 格式不同,这就是为什么不起作用。

[
    {
        "name": "19860",
        "header": {
            "start_time": "1519270200",
            "end_time": "1519299000",
            "state": "lunch"
        },
        "alerts": "1",
        "venue": {
            "location": "Delhi, India",
            "timezone": "+05:30"
        },
        "srs_category": [
            0,
            1
        ]
    },
    {
        "name": "19861",
        "header": {
            "start_time": "1519270200",
            "end_time": "1519299000",
            "state": "Dinner"
        },
        "alerts": "1",
        "venue": {
            "location": "Mumbai, India",
            "timezone": "+05:30"
        },
        "srs_category": [
            1,
            3
        ]
    },
    {
        "name": "19862",
        "header": {
            "start_time": "1519270200",
            "end_time": "1519299000",
            "state": "lunch"
        },
        "alerts": "1",
        "venue": {
            "location": "Surat, India",
            "timezone": "+05:30"
        },
        "srs_category": [
            0,
            2
        ]
    }
]

我想找到 srs_category 包含 1 的对象。我知道可以通过循环和条件来实现。但我想通过 NSPredicate。如果可能,请帮助我。

谢谢。

最佳答案

这是使用 SwiftyJSON 的简单方法:

    let filtered = JSON(yourArray).arrayValue.filter({
        $0["srs_category"].arrayValue.map({ $0.intValue }).contains(1)
    })

关于ios - 在 SwiftyJson 中过滤数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48925619/

相关文章:

javascript - jQuery .getJSON() 不解析所有对象

python - 'dict' 对象没有属性 'read'

json - '_InternalLinkedHashMap<String, dynamic >' is not a subtype of type ' Iterable<dynamic>' in type cast

ios - 结构初始值设定项不显示?

ios - 如何在 UIDatePicker 上设置时区?

swift - 如何使用 TextField 动态滚动

ios - 检查互联网连接 queryForTable Parse

ios - 如何知道用户点击了 TextView 的哪一行

ios - uiscrollview 滞后于渐变单元格背景

c++ - 无法为arm64和x86_64架构构建opencv ios框架