2

This is how my JSON nested DB looks:

enter image description here

Its a pets root that has multiple unique IDs. Then each ID represents a pet with some properties, some of them (like feedingList and walkingList) also contain more values inside that represent meals/walks and their time and completion Boolean.

I have written code in Swift to retrieve some of the values, but I have problem reaching those fList and wList.

func getPetInfo() {

        let refMeals = reff.child("pets")
        refMeals.observe(.value, with: { (snapshot) in

            if let snapCast = snapshot.value as? [String:AnyObject]{
                for snap in snapCast{
                    if self.currentUser.pets.contains(snap.key) {
                        if let x = snap.value["name"] as? String{
                            self.tempPet.name = x
                            print(x)

                        }
                        if let y = snap.value["type"] as? String{
                            //self.tempPet.type = y
                            print(y)

                        }
                        if let z = snap.value["age"] as? Int{
                            //self.tempPet.age = z
                            print(z)
                        }
                        print(snap.value["fList"])
                         if let w = snap.value["fList"] as? [String:AnyObject]{
                            for snippete in w {
                                if let a = snippete.value["time"] as? String{
                                    //self.tempPet.feedingList.append(a)
                                    print(a)
                                }
                                if let b = snippete.value["doneForToday"] as? Bool{
                                    //self.isItDoneForToday.append(b)
                                    print(b)
                                }
                            }
                        }
                        if let r = snap.value["wlist"] as? [String:AnyObject]{
                            for snippete in r {
                                if let c = snippete.value["time"] as? String{
                                    //self.tempPet.feedingList.append(a)
                                    print(c)
                                }
                                if let d = snippete.value["doneForToday"] as? Bool{
                                    //self.isItDoneForToday.append(b)
                                    print(d)
                                }
                            }
                        }
                        //self.petArray.append(self.tempPet)
                    }
                }
            }
        }) { (error) in
            print(error.localizedDescription)
        }
    }

It all prints out okay for name, age and type, but when it comes to this line of code:

 if let w = snap.value["fList"] as? [String:AnyObject]
...

It doesn't let it and nothing prints out. Can you tell me where I'm wrong? I'm clearly having trouble understanding values and arrays of this JSON tree. It's a mess when I'm trying to understand what to get and convert to what. Thanks in advance.

This is what gets printed out:

blasted
cat
3
Optional(<__NSArrayM 0x60800024f000>(
{
    doneForToday = 0;
    time = "12:00";
}
)
)  
2
  • It seems fList and wList are Arrays not Dictionaries ! Commented Oct 4, 2016 at 15:26
  • What do you mean? I mean in the same way "pets" (in my database on the first picture up there ) is presented as a dictionary with [strings that are IDs : AnyObject that has more stuff in], i wanted those fList elements to represent as [Strings that are number: AnyObject that has more stuff in] Commented Oct 4, 2016 at 20:59

1 Answer 1

1

Try this:-

 if let r = snap.value["wlist"] as? [Int:AnyObject]{
                        for snippete in r {
                            if let c = snippete.value["time"] as? String{
                                //self.tempPet.feedingList.append(a)
                                print(c)
                            }
                            if let d = snippete.value["doneForToday"] as? Bool{
                                //self.isItDoneForToday.append(b)
                                print(d)
                            }
                        }
                    }
                    //self.petArray.append(self.tempPet)
                }
Sign up to request clarification or add additional context in comments.

5 Comments

No, it doesn't fix the problem. I've set the value of those "0" and "1" ... to be a String, so it's not an Int.
@frank-van-puffelen can you help me out here?
can you think of any other reason why this doesnt work. Im stuck on this and dont know how to proceed
Yes they work. I updated the question with console prints
@Dravidian Could you please have a look at my question as I am struggling to understand how to dig deeper into the snapshot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.