如何检查json Object数组中多个键的相同值? [英] How to check same values for the multiple keys in array of json Object?

查看:363
本文介绍了如何检查json Object数组中多个键的相同值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Swift3处理日历.我知道可以为不同的键设置相同的值.

I am working on calendar using Swift3. I know it's possible to set same values for different keys.

我将此NSArray称为JSON个对象的Dictionary:

if parsedData["status"] as! Int == 200 {

    if let Streams = parsedData["data"] as! [AnyObject]? {
       for Stream in Streams {
           print(Stream) 
       }



Stream result: {
"-" = 4;
1 = X;
10 = H;
11 = X;
12 = X;
13 = X;
14 = X;
15 = X;
16 = X;
17 = H;
18 = X;
19 = X;
2 = X;
20 = X;
21 = X;
22 = X;
23 = L;
24 = H;
25 = X;
26 = L;
27 = "-";
28 = "-";
29 = "-";
3 = H;
30 = "-";
4 = X;
5 = X;
6 = X;
7 = X;
8 = X;
9 = X;
H = 4;
L = 2;
X = 20;
blank = "";
classid = id;
classname = " (A)";
stdid = 1;
stdnm = "name"; }

这里1到31是取决于月份的常数天. X H L 是树叶,普通树叶和普通树叶.我需要检查x,h,l值并获取日期.

Here 1 to 31 are constants days of depend on month. X H L are leaves , normal leaves, and normal. I need to check x, h , l values and getting dates.

但是,我不知道确切的逻辑来检查值是否相同,然后将其值添加到相同的键中.

However, I don't know exactly the logic to check if the values is the same, then add its value to the same key.

请帮助我.谢谢你

推荐答案

请检查:

这是您的JSON格式

if let streams = parsedData["data"] as! [AnyObject]? {
    for stream in streams {
        let dict = stream as? [AnyHashable: Any]
        print(getValue(key: "H", dict: dict!))
        print(getValue(key: "L", dict: dict!))
        print(getValue(key: "X", dict: dict!))
    }
}

func getValue(key: String, dict: [AnyHashable: Any]) -> [Int] {
    return dict.filter { $0.value as? String == key }.flatMap { Int(String(describing: $0.key)) }
}

以下是字典

let dict = ["-": 4, 1: "X", 10: "H", 11: "X", 12: "X", 13: "X", 14: "X",
           15: "X", 16: "X", 17: "H", 18: "X", 19: "X", 2: "X", 20: "X",
           21: "X", 22: "X", 23: "L", 24: "H", 25: "X", 26: "L", 27: "-",
           28: "-", 29: "-", 3: "H", 30: "-", 4: "X", 5: "X", 6: "X", 7: "X",
           8: "X", 9: "X", "H": 4, "L": 2, "X": 20] as [AnyHashable : Any]

选项1 传递密钥并返回日期数组

Option 1 Pass the key and will return dates array

    print(getValue(key: "H", dict: dict)) //[17, 10, 24, 3]
    print(getValue(key: "L", dict: dict)) //[26, 23]
    print(getValue(key: "X", dict: dict)) //[12, 25, 14, 20, 5, 15, 7, 11, 13, 16, 19, 22, 21, 9, 18, 2, 6, 4, 1, 8]

    func getValue(key: String, dict: [AnyHashable: Any]) -> [Int] {
        return dict.filter { $0.value as? String == key }.flatMap { $0.key as? Int}
    }

选项2 传递多个键,并将返回带有与每个键相关联的date数组的字典

Option 2 Pass multiple keys and will return dictionary with dates array associated with each key

    print(getValue2(keys: ["H","L","X"], dict: dict))
    //["H": [17, 24, 3, 10], "L": [23, 26], "X": [14, 20, 13, 19, 21, 9, 6, 1, 12, 25, 5, 15, 7, 11, 16, 22, 18, 2, 4, 8]]

    func getValue2(keys: [String], dict: [AnyHashable: Any]) -> [String: [Int]] {
        var returnValue = [String: [Int]]()
        for key in keys {
            returnValue[key] = [Int]()
        }

        dict.forEach {
            if let v = $0.value as? String, returnValue[v] != nil, let k = $0.key as? Int {
                returnValue[v]!.append(k)
            }
        }
        return returnValue
    }

这篇关于如何检查json Object数组中多个键的相同值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆