【问题标题】:how to deserialize this hashmap of realtime firebase database如何反序列化实时 Firebase 数据库的哈希图
【发布时间】:2020-09-23 00:47:49
【问题描述】:

我有一个 firebase 数据库架构,我想在其中解析和反序列化

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 + '\'' +
                '}';
    }



}

但每次我使用解析数据快照时

Program program = dataSnapshot.getValue(Program.class);

program.getSponsersMap() 始终等于 null

]

https://i.stack.imgur.com/fe9AU.png

【问题讨论】:

    标签: android firebase firebase-realtime-database


    【解决方案1】:

    我在 build.gradle 中使用 Gson 转换我的数据:

    dependencies {
        implementation 'com.google.code.gson:gson:2.8.6'
    }
    

    在我的代码中:

    JsonElement jsonElement = gson.toJsonTree(dataSnapshot.getValue());
    Program program = gson.fromJson(jsonElement, Program.class);
    

    它对我有用,firebase 使用该方法在我的情况下不起作用

    此外,在您的 Program 类中,您必须将 Sponser 转换为 List 而不是 HashMap,如果您从 Firebase 导出 RAW json,您必须将对象 Sponser 视为 List

    【讨论】:

    • 对不起,我把 getData() 改成了 getValue()
    • 很高兴为您提供帮助:)。如果您愿意,请不要忘记标记为帮助社区的解决方案
    【解决方案2】:

    从您的 JSON 中,Firebase 会将结构识别为数组,因此这意味着在 Java 类中它会映射到 List。

    所以:

    private List<Sponser> sponsers = new List<>();
    

    如果您确实希望将其映射到 Map,请确保键是字母数字,并在它们前面加上一个短的固定值,例如 s0、s1 等。

    【讨论】:

    • 这是解决方案的一部分,但对我有用的完整解决方案是:JsonElement jsonElement = gson.toJsonTree(dataSnapshot.getValue());程序program = gson.fromJson(jsonElement, Program.class);
    猜你喜欢
    • 2012-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-21
    • 1970-01-01
    • 2019-09-01
    • 2019-06-13
    • 1970-01-01
    相关资源
    最近更新 更多