0

I am trying to parse following JSON using Firebase:

   {
          "organizations" : {
            "-KS5bLCjQmNSQZDIQkTE" : {
              "about" : {
                "address" : "teka",
                "city" : "sns",
                "country" : "South Africa",
                "created_at" : 1474358502482,
                "name" : "diverse",
                "pincode" : "167377",
                "state" : "punjab",
                "utc" : "2016-09-20 08:01:41"
              },
              "department" : {
                "-KS9LUpZWo5UqtWKJwJ8" : {
                  "address" : "best",
                  "name" : "test"
                }
              }
            }
          }
   }

Here is my Java code:

@IgnoreExtraProperties
public class Organizations  {

  About about;
  Department department;

   public String getDepartment() {
     return department;
   }

   public static class Department{
      public String address;
      public String name;

      public String getName() {
       return name;
      }

     public String getAddress() {
       return address;
     }

   }

}

FirebaseRecyclerAdapter

 fbOrganizationRecycleAdapter = new FirebaseRecyclerAdapter<Organizations, OrganizationHolder>
                (Organizations.class, R.layout.list_items_orgnization,OrganizationHolder.class,lastFifty) 
                {
                     @Override 
                     protected void populateViewHolder(OrganizationHolder viewHolder, final Organizations model, int position) {
                        Log.e(TAG, "test " + model.getDepartment().getName());
                     }
                };

Everything is parsing fine except Department. It's returning me null department when I am trying to get model.getDepartment().getName();.

If the data is like the following JSON, it works fine:

   {
          "organizations" : {
            "-KS5bLCjQmNSQZDIQkTE" : {
              "about" : {
                "address" : "teka",
                "city" : "sns",
                "country" : "South Africa",
                "created_at" : 1474358502482,
                "name" : "diverse",
                "pincode" : "167377",
                "state" : "punjab",
                "utc" : "2016-09-20 08:01:41"
              },
              "department" : {

                  "address" : "best",
                  "name" : "test"

              }
            }
          }
   }

I think the issue is with the ID. Please let me know how to parse ID "-KS9LUpZWo5UqtWKJwJ8" from the following JSON:

 "department" : {
            "-KS9LUpZWo5UqtWKJwJ8" : {
              "address" : "best",
              "name" : "test"
            }
          }
AL.
  • 36,815
  • 10
  • 142
  • 281
Munish Kapoor
  • 3,141
  • 2
  • 26
  • 41
  • Please provide the minimum code that reproduces the issue. As it now stands, we don't know what the `Department` class looks like nor how you actually read the data (with some kind of listener or how you set up the reference for the adapter). – Frank van Puffelen Sep 21 '16 at 02:22
  • You can write `JsonDeserializer` or `TypeAdapterFactory` to parse it on your own. Here is a nice example from Jesse Wilson (Gson contributor) http://stackoverflow.com/a/11272452/1726166 – ytRino Sep 21 '16 at 03:10

1 Answers1

1

Try this:

class DepartmentChild{
   public String address;
   public String name;

   public String getName(){
      return this.name;  
   }
}

public class Organizations  {
  public About about;
  public Map<String,DepartmentChild>  department;

  public Map<String, DepartmentChild> getDepartment() {
    return department;
  }

  public void setDepartment(Map<String, DepartmentChild> department){
    this.department = department;
  }
}

Example

void test(Organizations model){
   for (Map.Entry<String, DepartmentChild > entry : model. getDepartment().entrySet()) {
      Log.e(TAG, "test " + entry.getKey()+ "/" + entry.getValue().getName());
   }
}

You can iterate the Map like this:

for (Map.Entry<String, DepartmentChild> entry : department.entrySet())
{
  System.out.println(entry.getKey() + "/" + entry.getValue().getName());
}
Munish Kapoor
  • 3,141
  • 2
  • 26
  • 41