How to Convert Firebase Data to Java Object...

How to Convert Firebase data to Java Object...?

There are two more way to get your data out of the Firebase DataSnapshot that don't require using a Map<String, Object>.

First appoach is to use the methods of DataSnapshot to traverse the children:

ref = FirebaseDatabase.getInstance().getReference("messages").limitToLast(10);
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot messageSnapshot: dataSnapshot.getChildren()) {
String name = (String) messageSnapshot.child("name").getValue();
String message = (String) messageSnapshot.child("message").getValue();
}
}

@Override
public void onCancelled(FirebaseError firebaseError) { }
});

In the above snippet we use getChildren() to get an Iterable of your messages. Then we use child("name") to get each specific child property.

The second approach is to use the built-in JSON-to-POJO serializer/deserializer. When you're sending the message list, the Message objects inside it are serialized to JSON and stored in Firebase.

To get them out of it again, you have to do the inverse:

ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot messageSnapshot: dataSnapshot.getChildren()) {
Message message = messageSnapshot.getValue(Message.class);
}
}

@Override
public void onCancelled(FirebaseError firebaseError) { }
});

In this second snippet, we're still using getChildren() to get at the messages, but now we deserialize them from JSON straight back into a Message object.

For a simple sample application using that last approach, have a look at Firebase's AndroidChat sample. It also shows how to efficiently deal with the list of messages (hint: FirebaseListAdapter).

How do I convert Firebase data into a Java object?

Your ChartColour doesn't meet the requirements for marshaling the data into class. Your class has to fulfill these 2 properties:

  1. The class must have a default constructor that takes no arguments.
  2. The class must define public getters for the properties to be assigned. Properties without a public getter will be set to their default value when an instance is deserialized.

In short, add public ChartColour() {}; to your class and a Getter per each parameter of your non-default constructor. Then call

ChartColour colour = userSnapshot.getValue(ChartColour.class);

If you want to use this, change it to ChartColour.this, assuming that is your outer class.

Convert value with child into Java Object in Firebase Android

Now I want to convert that value into a Java object. Can I do that?

Of course you can! Actually I have answered that question too. So to solve this, first you need to create two POJO (model) classes:

class MailId {
public String email, name;

MailId() {}
}

And

class MailText {
public String subject, title;

MailText() {}
}

To get that data as objects of MailId and MailText classes, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference newRef = rootRef.child("new");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.child("mailID").getChildren()) {
MailId mailId = ds.getValue(MailId.class);
Log.d("TAG", mailId.email + " / " + mailId.name);
}
for(DataSnapshot ds : dataSnapshot.child("mailText").getChildren()) {
MailText mailText = ds.getValue(MailText.class);
Log.d("TAG", mailText.subject + " / " + mailText.title);
}
}

@Override
public void onCancelled(DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
newRef.addListenerForSingleValueEvent(valueEventListener);

Convert Firebase Data Object to Array

To convert the object to an array, you could do something like this:

const obj = {
jyIibta0UWaRF2: {Name: "Value1", Surname: "Value2"},
Oy8r5SEYacKKM2: {Name: "Value3", Surname: "Value4"}
}

let data = [];
Object.keys(obj).forEach((key) => {
data.push(["Name="+obj[key].Name, "Surname="+obj[key].Surname]);
});

console.log(data[1][1]);

Converting Firebase json to Java object

Your MobileNumber was saved as a long value and you're trying to retrieve it as a String. You can change it's data type on your POJO:

public class User {
private String EmailId;
private long MobileNumber;
private String UserName;
private ArrayList<UserLocationInfo> userLocationInfos;

//Constructor, getters and setters are below.
}

Or turning into a String on your Database, by adding quotation marks:

"MobileNumber": "1234567890",
"UserName": "Firstname Lastname"

How to retrieve an object from firebase database?

I think this may come from the fact that your field is called fdName, while the property in the database is called foodName.

Firebase uses either the getter and setter to determine the name of the property, or if those are missing, the name of the field. So it's looking for a property called fdName in the database.

The solution is to rename your field to match the property name in the database:

public class FoodItem {
private String foodName; // br> private String amount;

public FoodItem(String foodName, String foodAmount) {
this.foodName = foodName;
this.amount=foodAmount;
}


public String getFoodName() {
return this.foodName; // br> }

public String getAmount() {
return this.amount;
}
}

How to convert firebase data (snake_case) to Java object (camelCase)

The problem was @SerializedName annotation. Firebase has its own annotation, which is @PropertyName.

It is important to be careful about getter name because annotation cares about its name too.
The property must be public too.

There is a perfect answer about that on this link.

Final state of my pojo;

@PropertyName("content")
public String content;
@PropertyName("user_name")
public String userName;


Related Topics



Leave a reply



Submit