如何反序列化此实时Firebase数据库哈希表 [英] how to deserialize this hashmap of realtime firebase database
问题描述
我有一个firebase数据库架构,我想在其中进行解析和反序列化
i have a firebase database schema, in which i wanna to parse and deserialize with
Program program = dataSnapshot.getValue(Program.class);
程序数据类为:
public class Program
{
@SerializedName("has_live_show")
@Expose
private Boolean has_live_show;
@SerializedName("id")
@Expose
private int id;
@SerializedName("live_url")
@Expose
private String live_url;
@SerializedName("title")
@Expose
private String title;
@SerializedName("sponsers")
@Expose
private Map<Integer, Sponser> sponsers=new HashMap<>();
public Program() {
}
public Program(Boolean has_live_show, int id, String live_url, String title, Map<Integer, Sponser> sponsers) {
super();
this.has_live_show = has_live_show;
this.id = id;
this.live_url = live_url;
this.title = title;
this.sponsers = sponsers;
}
public Boolean getHas_live_show() {
return has_live_show;
}
public void setHas_live_show(Boolean has_live_show) {
this.has_live_show = has_live_show;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLive_url() {
return live_url;
}
public void setLive_url(String live_url) {
this.live_url = live_url;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Map<Integer, Sponser> getSponsersMap() {
return sponsers;
}
public void setSponsersMap(Map<Integer, Sponser> sponsers) {
this.sponsers = sponsers;
}
@Override
public String toString() {
return "Program{" +
"hasLiveShow='" + has_live_show + '\'' +
", id=" + id +
", liveUrl='" + live_url + '\'' +
", title='" + title + '\'' +
", sponsers=" + sponsers +
'}';
}
}
和赞助者数据类是
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Sponsor {
@SerializedName("image")
@Expose
private String image;
@SerializedName("logo")
@Expose
private String logo;
public Sponser() {
}
public Sponser(String image, String logo) {
this.image = image;
this.logo = logo;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getLogo() {
return logo;
}
public void setLogo(String logo) {
this.logo = logo;
}
@Override
public String toString() {
return "Sponser{" +
"image='" + image + '\'' +
", logo='" + logo + '\'' +
'}';
}
}
但是每次我使用解析数据快照时
but every time I parse the data snapshot using
Program program = dataSnapshot.getValue(Program.class);
program.getSponsersMap()
始终等于null
]
https://i.stack.imgur.com/fe9AU.png
推荐答案
我在build.gradle中使用Gson转换我的数据:
I use Gson for convert my data, in build.gradle:
dependencies {
implementation 'com.google.code.gson:gson:2.8.6'
}
在我的代码中:
JsonElement jsonElement = gson.toJsonTree(dataSnapshot.getValue());
Program program = gson.fromJson(jsonElement, Program.class);
它对我有用,在我的情况下,firebase使用该方法不起作用
and it works for me, firebase use the method does not work in my case
在程序类中,还必须将Sponser转换为List而不是HashMap,如果从Firebase导出RAW json,则必须将对象Sponser转换为List
Also in your Program class you must be convert Sponser as List instead of HashMap, if you exports RAW json from Firebase, you must see the object Sponser as List
这篇关于如何反序列化此实时Firebase数据库哈希表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!