1

In this case, I want to retrieve the m1 key by referring to it's child key with the value of amoxicillin from firebase. I haven't tried to code it because i don't know where to begin.

{
  "Medicines" : {
    "m1" : {
      "class" : "antibiotic",
      "name" : "amoxcillin"
    },
    "m2" : {
      "class" : "analgesic",
      "name" : "paracetamol"
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Possible duplicate of [How to iterate over a JSONObject?](https://stackoverflow.com/questions/9151619/how-to-iterate-over-a-jsonobject) – denvercoder9 Mar 04 '18 at 15:57

2 Answers2

0

Let's say you have an array of medicines:

Medicine[] medicines

You could iterate over each object in it and check if the class is amoxicillin or not:

for (int i = 0; i < medicines.length; i++) {
    Medicine medicine = medicines[i];

    if (medicine.getClass().equals("amoxicillin")) {
        // do smth here.. add the medicine to a separate array
        // or do smth with the index
        // for your case you should iterate over the jsonObject
        // and catch the key here
    }
}
denvercoder9
  • 2,979
  • 3
  • 28
  • 41
0

Have a field for the parent in the child class(Model) then ie

    parent : { 
           child : { 
                  ...
                  parent: "parent_key" 
                  ...
                   }
     }