简体   繁体   中英

Parsing my SWIFT Dictionary

I am querying a JSON database of zombies and it returns them as a Dictionary. I don't know how to mutate it with SWIFT 3

Here's the query::

func getZombieAttackGroupFromDatabase () {
        ref?.child("Attacker Group").child((self.userParty?.leadID)!).observeSingleEvent(of: .value, with: { (snapshot) in
            // Get data
            print("PULLING DATA")
            if let value = snapshot.value as? NSDictionary{

                // break data into an Array of Zombie Structs

          //      print(value)

                for zombieID in value.allKeys {
                    print(value[zombieID])

                    let thisZombieID = zombieID
                    let thisZombieGroup = value[zombieID]["Group"]
                }



            } else {

            }
            // ...
        }) { (error) in
            print(error.localizedDescription)
        }


}

this part: let thisZombieGroup = value[zombieID]["Group"] isn't being recognized. How do I access group? If i get that, i can modify to the other components.

Here's the return :

{
    "-KrNSmv64Ia32g5nw1L9" =     {
        Group = 15;
        Health = 250;
        "Is Undead" = true;
        Location = 1;
        Number = 0;
    };
    "-KrNSmv64Ia32g5nw1LA" =     {
        Group = 11;
        Health = 250;
        "Is Undead" = true;
        Location = 5;
        Number = 1;
    };
    "-KrNSmv64Ia32g5nw1LB" =     {
        Group = 2;
        Health = 250;
        "Is Undead" = true;
        Location = 3;
        Number = 2;
    };
    "-KrNSmv776r9eO6t7CY0" =     {
        Group = 14;
        Health = 250;
        "Is Undead" = true;
        Location = 0;
        Number = 3;
    };
    "-KrNSmv776r9eO6t7CY1" =     {
        Group = 0;
        Health = 250;
        "Is Undead" = true;
        Location = 4;
        Number = 4;
    };
}

As you can see, each of the Structs has a parent that is an automatically generated ID. I don't know how to access it.

How can I access each element from item 1? I need the parent auto-key "-KrNSmv64Ia32g5nw1L9" and each child value.

"-KrNSmv64Ia32g5nw1L9" =     {
        Group = 15;
        Health = 250;
        "Is Undead" = true;
        Location = 1;
        Number = 0; 

Cast value to a proper Swift dictionary, not NSDictionary .

if let value = snapshot.value as? [String:Any] if let value = snapshot.value as? [String:Any] .

You just have to iterate through the keys of the dictionaries, get the embedded dictionary using the key value and then parse the "zombie data".

for key in value.keys {
    if let zombieData = value[key] as? [String:Any] {
        zombieData
        if let group = zombieData["Group"] as? Int, let health = zombieData["Health"] as? Int, let isUndead = zombieData["Is Undead"] as? Bool, let location = zombieData["Location"] as? Int, let number = zombieData["Number"] as? Int {
            //use the parsed data
        }
    }
}

Try this:

               for zombieID in value.allKeys {
                    print(value[zombieID])

                    let thisZombieID = zombieID
                    if let zombieValues = value[zombieID] as? [String:Any] {
                         let thisZombieGroup = zombieValues["Group"] as? Int
                    }
                }

First, Thank you so much to Woof and David. It was a combination of both of your ideas that got it to work.

    func getZombieAttackGroupFromDatabase () {
            ref?.child("Attacker Group").child((self.userParty?.leadID)!).observeSingleEvent(of: .value, with: { (snapshot) in
                // Get data
                print("PULLING DATA")
                if let value = snapshot.value as? [String:Any]{

                    // break data into an Array of Zombie Structs

                    for zombieID in value.keys {
                        let thisZombieID = zombieID
                        print(thisZombieID)

                        let zombieValues = value[zombieID] as? [String:Any]

                        let thisZombieGroup = zombieValues?["Group"] as! String
                        print(thisZombieGroup)

                    }
                } else {

                }
                // ...
            }) { (error) in
                print(error.localizedDescription)
            }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM