Import Firebase Data Into Android Class
Solution 1:
This is the JSON for one of the dinosaurs in that database:
"bruhathkayosaurus":{"appeared":-70000000,"height":25,"length":44,"order":"saurischia","vanished":-70000000,"weight":135000},
The DinosaurFacts
class you're using only has fields+getters for these properties: height
, length
, weight
. So when the Firebase tries to deserialize the JSON into a DinosaurFacts
object, it complains about the unmapped properties.
The simplest way to get rid of the error is by telling Jackson (which Firebase uses internally to map between JSON and Java) to ignore any unmapped properties it encounters in the JSON:
@JsonIgnoreProperties(ignoreUnknown = true)publicstaticclassDinosaurFacts {
But be careful with this. If you now write a DinosaurFacts
object back into the database, it will only have height
, length
and weight
properties. It will not have: appeared
, order
or vanished
. In fact: even if the properties did exist in the database before, writing this object back will delete them.
So the proper solution is to map all the properties from the JSON structure into the Java class:
publicstaticclassDinosaurFacts {
long appeared, vanished;
double height, length, weight;
String order;
publiclonggetAppeared() {
return appeared;
}
publiclonggetVanished() {
return vanished;
}
publicdoublegetHeight() {
return height;
}
publicdoublegetLength() {
return length;
}
publicdoublegetWeight() {
return weight;
}
public String getOrder() {
return order;
}
}
Post a Comment for "Import Firebase Data Into Android Class"