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"
}
}
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());
}