我正在尝试为我的Firebase实时数据库构建POJO。 我是否根据我的实时数据库正确执行了操作?下方链接
detailData,detailContent,detailTitleContent,isDetail,titleContent他们在各处都命名相同,只是其中的文字不同。
public class POJO {
private String titleContent;
private String detailContent;
private String detailTitleContent;
private List<String> detailData = new ArrayList<>();
private List<String> textInfo = new ArrayList<>();
private boolean isDetail;
private boolean isList;
public POJO() {
}
public POJO(String titleContent, String detailContent, String
detailTitleContent, List<String> detailData, List<String> textInfo,
boolean isDetail, boolean isList) {
this.titleContent = titleContent;
this.detailContent = detailContent;
this.detailTitleContent = detailTitleContent;
this.detailData = detailData;
this.textInfo = textInfo;
this.isDetail = isDetail;
this.isList = isList;
}
public String getTitleContent() {
return titleContent;
}
public String getDetailContent() {
return detailContent;
}
public String getDetailTitleContent() {
return detailTitleContent;
}
public List<String> getDetailData() {
return detailData;
}
public List<String> getTextInfo() {
return textInfo;
}
public boolean isDetail() {
return isDetail;
}
public boolean isList() {
return isList;
}
}
答案 0 :(得分:2)
基于以下响应(您已提供),我将创建POJO类
{
"datas": [{
"detailData": [{
"detailContent": "<p>LOTS of information</p>",
"detailTitleContent": "Title"
}, {
"detailContent": "<p>Lots of more information!</p>",
"detailTitleContent": "Second Title"
}],
"isDetail": false,
"titleContent": "Last Title"
}]
}
因此,查看此响应,您可以看到您的第一个(我们的名字是“ MyPojo”类)将具有一个“数据”对象数组。
public class MyPojo
{
private Datas[] datas;
public Datas[] getDatas (){
return datas;
}
public void setDatas (Datas[] datas){
this.datas = datas;
}
}
现在我们必须为“数据”创建一个模型对象:
public class Datas
{
private String isDetail;
private String titleContent;
private DetailData[] detailData;
public String getIsDetail (){
return isDetail;
}
public void setIsDetail (String isDetail){
this.isDetail = isDetail;
}
public String getTitleContent (){
return titleContent;
}
public void setTitleContent (String titleContent){
this.titleContent = titleContent;
}
public DetailData[] getDetailData (){
return detailData;
}
public void setDetailData (DetailData[] detailData){
this.detailData = detailData;
}
}
最后但并非最不重要的另一种数组“ DetailData”模型:
public class DetailData
{
private String detailTitleContent;
private String detailContent;
public String getDetailTitleContent (){
return detailTitleContent;
}
public void setDetailTitleContent (String detailTitleContent){
this.detailTitleContent = detailTitleContent;
}
public String getDetailContent (){
return detailContent;
}
public void setDetailContent (String detailContent){
this.detailContent = detailContent;
}
}
从这里开始,您应该为您的JSON响应提供完整的Pojo,并可以对其进行处理。只想指出以下两点对您有利:
1。。我强烈建议您阅读以下教程Android JSON Parsing Tutorial,并密切注意 [和{–(方括号和花括号)之间的区别您想深入了解 JSONArray 和 JSONObject 。
2 。使用JSONLint来验证您的JSON响应(有时会很有用),还使用Convert XML or JSON to Java Pojo Classes - Online工具根据JSON响应生成Pojo类(我在本文中亲自使用了它)案件)。这样做的主要好处是准确性高,只需不到1分钟即可完成复制和实施。
祝您好运,如果您需要进一步的帮助,请告诉我:)