迭代包含其他对象实例的Object

时间:2017-05-22 10:24:01

标签: android loops

我正在为特定情况寻找一个优雅的解决方案。 我有一个JSON文件 在我解析它之后,我得到一个类对象,其中包含作为其他对象实例的字段。 这个类看起来像这样:

public class TaboolaResults {

@SerializedName("session")
@Expose
private String session;

@SerializedName("App-Below-Main-Column-Thumbnails-HP")
@Expose
private AppBelowMainColumnThumbnailsHP appBelowMainColumnThumbnailsHP;

@SerializedName("App-Below-Main-Column-Thumbnails-HP-2")
@Expose
private AppBelowMainColumnThumbnailsHP2 appBelowMainColumnThumbnailsHP2;

@SerializedName("App-Below-Main-Column-Thumbnails-HP-3")
@Expose
private AppBelowMainColumnThumbnailsHP3 appBelowMainColumnThumbnailsHP3;

@SerializedName("App-Below-Main-Column-Thumbnails-HP-4")
@Expose
private AppBelowMainColumnThumbnailsHP4 appBelowMainColumnThumbnailsHP4;

@SerializedName("App-Below-Main-Column-Thumbnails-HP-5")
@Expose
private AppBelowMainColumnThumbnailsHP5 appBelowMainColumnThumbnailsHP5;

@SerializedName("App-Below-Main-Column-Thumbnails-HP-6")
@Expose
private AppBelowMainColumnThumbnailsHP6 appBelowMainColumnThumbnailsHP6;

@SerializedName("App-Below-Main-Column-Thumbnails-HP-7")
@Expose
private AppBelowMainColumnThumbnailsHP7 appBelowMainColumnThumbnailsHP7;

@SerializedName("App-Below-Main-Column-Thumbnails-HP-8")
@Expose
private AppBelowMainColumnThumbnailsHP8 appBelowMainColumnThumbnailsHP8;

@SerializedName("App-Below-Main-Column-Thumbnails-HP-9")
@Expose
private AppBelowMainColumnThumbnailsHP9 appBelowMainColumnThumbnailsHP9;

public String getSession() {
    return session;
}

public void setSession(String session) {
    this.session = session;
}

public AppBelowMainColumnThumbnailsHP getAppBelowMainColumnThumbnailsHP() {
    return appBelowMainColumnThumbnailsHP;
}

public void setAppBelowMainColumnThumbnailsHP(AppBelowMainColumnThumbnailsHP appBelowMainColumnThumbnailsHP) {
    this.appBelowMainColumnThumbnailsHP = appBelowMainColumnThumbnailsHP;
}

public AppBelowMainColumnThumbnailsHP2 getAppBelowMainColumnThumbnailsHP2() {
    return appBelowMainColumnThumbnailsHP2;
}

public void setAppBelowMainColumnThumbnailsHP2(AppBelowMainColumnThumbnailsHP2 appBelowMainColumnThumbnailsHP2) {
    this.appBelowMainColumnThumbnailsHP2 = appBelowMainColumnThumbnailsHP2;
}

public AppBelowMainColumnThumbnailsHP3 getAppBelowMainColumnThumbnailsHP3() {
    return appBelowMainColumnThumbnailsHP3;
}

public void setAppBelowMainColumnThumbnailsHP3(AppBelowMainColumnThumbnailsHP3 appBelowMainColumnThumbnailsHP3) {
    this.appBelowMainColumnThumbnailsHP3 = appBelowMainColumnThumbnailsHP3;
}

public AppBelowMainColumnThumbnailsHP4 getAppBelowMainColumnThumbnailsHP4() {
    return appBelowMainColumnThumbnailsHP4;
}

public void setAppBelowMainColumnThumbnailsHP4(AppBelowMainColumnThumbnailsHP4 appBelowMainColumnThumbnailsHP4) {
    this.appBelowMainColumnThumbnailsHP4 = appBelowMainColumnThumbnailsHP4;
}

public AppBelowMainColumnThumbnailsHP5 getAppBelowMainColumnThumbnailsHP5() {
    return appBelowMainColumnThumbnailsHP5;
}

public void setAppBelowMainColumnThumbnailsHP5(AppBelowMainColumnThumbnailsHP5 appBelowMainColumnThumbnailsHP5) {
    this.appBelowMainColumnThumbnailsHP5 = appBelowMainColumnThumbnailsHP5;
}

public AppBelowMainColumnThumbnailsHP6 getAppBelowMainColumnThumbnailsHP6() {
    return appBelowMainColumnThumbnailsHP6;
}

public void setAppBelowMainColumnThumbnailsHP6(AppBelowMainColumnThumbnailsHP6 appBelowMainColumnThumbnailsHP6) {
    this.appBelowMainColumnThumbnailsHP6 = appBelowMainColumnThumbnailsHP6;
}

public AppBelowMainColumnThumbnailsHP7 getAppBelowMainColumnThumbnailsHP7() {
    return appBelowMainColumnThumbnailsHP7;
}

public void setAppBelowMainColumnThumbnailsHP7(AppBelowMainColumnThumbnailsHP7 appBelowMainColumnThumbnailsHP7) {
    this.appBelowMainColumnThumbnailsHP7 = appBelowMainColumnThumbnailsHP7;
}

public AppBelowMainColumnThumbnailsHP8 getAppBelowMainColumnThumbnailsHP8() {
    return appBelowMainColumnThumbnailsHP8;
}

public void setAppBelowMainColumnThumbnailsHP8(AppBelowMainColumnThumbnailsHP8 appBelowMainColumnThumbnailsHP8) {
    this.appBelowMainColumnThumbnailsHP8 = appBelowMainColumnThumbnailsHP8;
}

public AppBelowMainColumnThumbnailsHP9 getAppBelowMainColumnThumbnailsHP9() {
    return appBelowMainColumnThumbnailsHP9;
}

public void setAppBelowMainColumnThumbnailsHP9(AppBelowMainColumnThumbnailsHP9 appBelowMainColumnThumbnailsHP9) {
    this.appBelowMainColumnThumbnailsHP9 = appBelowMainColumnThumbnailsHP9;
}
}

我实现了我所需要的非常不优雅的方式是:

 private void extractTaboolaIntoFeed(final TaboolaResults results, final ArrayList<IFlexible> itemResults) {

    int i = 0;
    int currentTaboolaItem = 1;

    if (currentTaboolaItem <= mNumberOfTaboolaItems) {

        AppBelowMainColumnThumbnailsHP appBelowMainColumnThumbnailsHP = results.getAppBelowMainColumnThumbnailsHP();

        for (; i < itemResults.size(); i++) {

            if (itemResults.get(i) instanceof AbstractItem) {

                AbstractItem abstractItem = (AbstractItem) itemResults.get(i);

                FeedItemType itemType = abstractItem.getItemType();
                if (itemType.equals(FeedItemType.TABOOLA)) {
                    mTaboolaDataSet.add(new TaboolaDataSetItem(abstractItem.getItemId(), appBelowMainColumnThumbnailsHP));
                    currentTaboolaItem++;
                    break;
                }
            }
        }
    }

    if (currentTaboolaItem <= mNumberOfTaboolaItems) {
        AppBelowMainColumnThumbnailsHP2 appBelowMainColumnThumbnailsHP2 = results.getAppBelowMainColumnThumbnailsHP2();

        for (i++; i < itemResults.size(); i++) {
            if (itemResults.get(i) instanceof AbstractItem) {
                AbstractItem abstractItem = (AbstractItem) itemResults.get(i);
                FeedItemType itemType = abstractItem.getItemType();
                if (itemType.equals(FeedItemType.TABOOLA)) {
                    mTaboolaDataSet.add(new TaboolaDataSetItem(abstractItem.getItemId(), appBelowMainColumnThumbnailsHP2));
                    currentTaboolaItem++;
                    break;
                }
            }
        }
    }

    if (currentTaboolaItem <= mNumberOfTaboolaItems) {
        AppBelowMainColumnThumbnailsHP3 appBelowMainColumnThumbnailsHP3 = results.getAppBelowMainColumnThumbnailsHP3();
        for (i++; i < itemResults.size(); i++) {
            if (itemResults.get(i) instanceof AbstractItem) {
                AbstractItem abstractItem = (AbstractItem) itemResults.get(i);
                FeedItemType itemType = abstractItem.getItemType();
                if (itemType.equals(FeedItemType.TABOOLA)) {
                    mTaboolaDataSet.add(new TaboolaDataSetItem(abstractItem.getItemId(), appBelowMainColumnThumbnailsHP3));
                    currentTaboolaItem++;
                    break;
                }
            }
        }
    }

    if (currentTaboolaItem <= mNumberOfTaboolaItems) {
        AppBelowMainColumnThumbnailsHP4 appBelowMainColumnThumbnailsHP4 = results.getAppBelowMainColumnThumbnailsHP4();
        for (i++; i < itemResults.size(); i++) {

            if (itemResults.get(i) instanceof AbstractItem) {

                AbstractItem abstractItem = (AbstractItem) itemResults.get(i);
                FeedItemType itemType = abstractItem.getItemType();
                if (itemType.equals(FeedItemType.TABOOLA)) {
                    mTaboolaDataSet.add(new TaboolaDataSetItem(abstractItem.getItemId(), appBelowMainColumnThumbnailsHP4));
                    currentTaboolaItem++;
                    break;
                }
            }
        }
    }

    if (currentTaboolaItem <= mNumberOfTaboolaItems) {
        AppBelowMainColumnThumbnailsHP5 appBelowMainColumnThumbnailsHP5 = results.getAppBelowMainColumnThumbnailsHP5();
        for (i++; i < itemResults.size(); i++) {

            if (itemResults.get(i) instanceof AbstractItem) {

                AbstractItem abstractItem = (AbstractItem) itemResults.get(i);
                FeedItemType itemType = abstractItem.getItemType();
                if (itemType.equals(FeedItemType.TABOOLA)) {
                    mTaboolaDataSet.add(new TaboolaDataSetItem(abstractItem.getItemId(), appBelowMainColumnThumbnailsHP5));
                    currentTaboolaItem++;
                    break;
                }
            }
        }
    }

    if (currentTaboolaItem <= mNumberOfTaboolaItems) {
        AppBelowMainColumnThumbnailsHP6 appBelowMainColumnThumbnailsHP6 = results.getAppBelowMainColumnThumbnailsHP6();
        for (i++; i < itemResults.size(); i++) {
            if (itemResults.get(i) instanceof AbstractItem) {
                AbstractItem abstractItem = (AbstractItem) itemResults.get(i);
                FeedItemType itemType = abstractItem.getItemType();
                if (itemType.equals(FeedItemType.TABOOLA)) {
                    mTaboolaDataSet.add(new TaboolaDataSetItem(abstractItem.getItemId(), appBelowMainColumnThumbnailsHP6));
                    currentTaboolaItem++;
                    break;
                }
            }
        }
    }

    if (currentTaboolaItem <= mNumberOfTaboolaItems) {
        AppBelowMainColumnThumbnailsHP7 appBelowMainColumnThumbnailsHP7 = results.getAppBelowMainColumnThumbnailsHP7();
        for (i++; i < itemResults.size(); i++) {
            if (itemResults.get(i) instanceof AbstractItem) {
                AbstractItem abstractItem = (AbstractItem) itemResults.get(i);
                FeedItemType itemType = abstractItem.getItemType();
                if (itemType.equals(FeedItemType.TABOOLA)) {
                    mTaboolaDataSet.add(new TaboolaDataSetItem(abstractItem.getItemId(), appBelowMainColumnThumbnailsHP7));
                    currentTaboolaItem++;
                    break;
                }
            }
        }
    }

    if (currentTaboolaItem <= mNumberOfTaboolaItems) {
        AppBelowMainColumnThumbnailsHP8 appBelowMainColumnThumbnailsHP8 = results.getAppBelowMainColumnThumbnailsHP8();
        for (i++; i < itemResults.size(); i++) {
            if (itemResults.get(i) instanceof AbstractItem) {
                AbstractItem abstractItem = (AbstractItem) itemResults.get(i);
                FeedItemType itemType = abstractItem.getItemType();
                if (itemType.equals(FeedItemType.TABOOLA)) {
                    mTaboolaDataSet.add(new TaboolaDataSetItem(abstractItem.getItemId(), appBelowMainColumnThumbnailsHP8));
                    currentTaboolaItem++;
                    break;
                }
            }
        }
    }

    if (currentTaboolaItem <= mNumberOfTaboolaItems) {
        AppBelowMainColumnThumbnailsHP9 appBelowMainColumnThumbnailsHP9 = results.getAppBelowMainColumnThumbnailsHP9();
        for (i++; i < itemResults.size(); i++) {
            if (itemResults.get(i) instanceof AbstractItem) {
                AbstractItem abstractItem = (AbstractItem) itemResults.get(i);
                FeedItemType itemType = abstractItem.getItemType();
                if (itemType.equals(FeedItemType.TABOOLA)) {
                    mTaboolaDataSet.add(new TaboolaDataSetItem(abstractItem.getItemId(), appBelowMainColumnThumbnailsHP9));
                    currentTaboolaItem++;
                    break;
                }
            }
        }
    }
}

我目前正在使用gson进行解析。 包含另一个对象实例的结果对象是从gson接收的模型。

每次解析json时,根据我从服务器获得的响应,类可以包含不同数量的对象。 因此,目标是迭代类的字段并检查类结果,并检查它是否具有结果对象或为null。

理论上的Idily:

获取类对象结果,迭代所有实例对象结果,如果object!= null,则执行somethig。

另一个可能的解决方案,如果有人知道是否/如何完成它是配置gson,即使文件包含不同的对象(具有不同名称的对象),make gson创建所有对象与具有相同名称的相等对象相同

任何人都可以告诉我更好的方法吗? 谢谢

1 个答案:

答案 0 :(得分:0)

结束将课程更改为:

public class TaboolaResults {

@SerializedName("session")
@Expose
private String session;

@SerializedName("App-Below-Main-Column-Thumbnails-HP")
@Expose
private AppBelowMainColumnThumbnailsHP appBelowMainColumnThumbnailsHP;

@SerializedName("App-Below-Main-Column-Thumbnails-HP-2")
@Expose
private AppBelowMainColumnThumbnailsHP appBelowMainColumnThumbnailsHP2;

@SerializedName("App-Below-Main-Column-Thumbnails-HP-3")
@Expose
private AppBelowMainColumnThumbnailsHP appBelowMainColumnThumbnailsHP3;

@SerializedName("App-Below-Main-Column-Thumbnails-HP-4")
@Expose
private AppBelowMainColumnThumbnailsHP appBelowMainColumnThumbnailsHP4;

@SerializedName("App-Below-Main-Column-Thumbnails-HP-5")
@Expose
private AppBelowMainColumnThumbnailsHP appBelowMainColumnThumbnailsHP5;

@SerializedName("App-Below-Main-Column-Thumbnails-HP-6")
@Expose
private AppBelowMainColumnThumbnailsHP appBelowMainColumnThumbnailsHP6;

@SerializedName("App-Below-Main-Column-Thumbnails-HP-7")
@Expose
private AppBelowMainColumnThumbnailsHP appBelowMainColumnThumbnailsHP7;

@SerializedName("App-Below-Main-Column-Thumbnails-HP-8")
@Expose
private AppBelowMainColumnThumbnailsHP appBelowMainColumnThumbnailsHP8;

@SerializedName("App-Below-Main-Column-Thumbnails-HP-9")
@Expose
private AppBelowMainColumnThumbnailsHP appBelowMainColumnThumbnailsHP9;

并简单地迭代所有结果:

int currentTaboola = 1;
        for (int i = 0; i < feedItems.size(); i++) {
            if (feedItems.get(i) instanceof TaboolaItem) {
                AbstractItem item = (AbstractItem) feedItems.get(i);
                AppBelowMainColumnThumbnailsHP taboolaResult = mGsonResults.getAppBelowMainColumnThumbnails(currentTaboola);
                mTaboolaDataSet.add(new TaboolaDataSetItem(item.getItemId(), taboolaResult));
                currentTaboola++;
            }
        }