Android java循环通过对象列表

时间:2015-04-20 20:39:29

标签: java android json list object

我的代码连接到Api,获取所有Json值并将它们全部存储在对象列表resultClass中。我如何遍历列表并显示对象的所有名称属性?

这是我正在使用的代码。 JSON值通过方法调用作为参数发送,名称为object。然后循环并获取所有对象并将它们存储在列表中。

public void onResponse(JSONObject object) {
        Log.i("gw2Log", "Json Response" + object);

        resultClass resultClass = new resultClass();

        try {
            resultClass.setCount(object.getInt("count"));
            resultClass.setPage(object.getInt("page"));
            resultClass.setLast_page(object.getInt("last_page"));
            resultClass.setTotal(object.getInt("total"));
            JSONArray list = new JSONArray(object.getString("results"));

            for (int i = 0; i < resultClass.getTotal(); i++) {
                JSONObject resultsObject = list.getJSONObject(i);

                resultClass.setData_id(resultsObject
                        .getInt("data_id"));
                resultClass.setName(resultsObject
                        .getString("name"));
                resultClass.setRarity(resultsObject
                        .getInt("rarity"));
                resultClass.setRestriction_level(resultsObject
                        .getInt("restriction_level"));
                resultClass.setImg(resultsObject
                        .getString("img"));
                resultClass.setType_id(resultsObject
                        .getInt("type_id"));
                resultClass.setSub_type_id(resultsObject
                        .getInt("sub_type_id"));
                resultClass.setPrice_last_changed(resultsObject
                        .getString("price_last_changed"));
                resultClass.setMax_offer_unit_price(resultsObject
                        .getInt("max_offer_unit_price"));
                resultClass.setMin_sale_unit_price(resultsObject
                        .getInt("min_sale_unit_price"));
                resultClass.setOffer_availability(resultsObject
                        .getInt("offer_availability"));
                resultClass.setSale_availability(resultsObject
                        .getInt("sale_availability"));
                resultClass.setSale_price_change_last_hour(resultsObject
                        .getInt("sale_price_change_last_hour"));
                resultClass.setOffer_price_change_last_hour(resultsObject
                        .getInt("offer_price_change_last_hour"));
            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        for(int i = 0; i < resultClass.total; i++) {
            Log.i("gw2Log", resultClass.name[i]);
        }
    }

这是我正在记录的Json响应

Json Response{"total":6,"last_page":1,"results":[{"sale_availability":0,"offer_availability":0,"img":"https:\/\/render.guildwars2.com\/file\/01D07FABAE26C0E5240892B00DA7AF90AB0EA022\/455828.png","rarity":7,"type_id":16,"sale_price_change_last_hour":0,"max_offer_unit_price":0,"data_id":19648,"price_last_changed":"2015-04-20 20:23:48 UTC","offer_price_change_last_hour":0,"name":"Gift of Twilight","min_sale_unit_price":0,"restriction_level":0,"sub_type_id":0},{"sale_availability":0,"offer_availability":0,"img":"https:\/\/render.guildwars2.com\/file\/CE3AF0B7B9BB6244726779F5B6A930541BA6C15F\/456031.png","rarity":5,"type_id":18,"sale_price_change_last_hour":0,"max_offer_unit_price":0,"data_id":49191,"price_last_changed":"2015-04-20 20:23:48 UTC","offer_price_change_last_hour":0,"name":"Twilight","min_sale_unit_price":0,"restriction_level":80,"sub_type_id":6},{"sale_availability":23,"offer_availability":20643,"img":"https:\/\/render.guildwars2.com\/file\/CE3AF0B7B9BB6244726779F5B6A930541BA6C15F\/456031.png","rarity":7,"type_id":18,"sale_price_change_last_hour":0,"max_offer_unit_price":27500000,"data_id":30704,"price_last_changed":"2015-04-20 20:17:57 UTC","offer_price_change_last_hour":0,"name":"Twilight","min_sale_unit_price":31959998,"restriction_level":80,"sub_type_id":6},{"sale_availability":0,"offer_availability":0,"img":"https:\/\/render.guildwars2.com\/file\/D04EF6FDE3DBC26E7BB109EB4F52057FEAD8619E\/699325.png","rarity":1,"type_id":4,"sale_price_change_last_hour":0,"max_offer_unit_price":0,"data_id":65578,"price_last_changed":"2015-04-20 20:23:48 UTC","offer_price_change_last_hour":0,"name":"Twilight Arbor Armor Box","min_sale_unit_price":0,"restriction_level":0,"sub_type_id":0},{"sale_availability":0,"offer_availability":0,"img":"https:\/\/render.guildwars2.com\/file\/666209104CCB024D53359C0EA0A299076E610771\/65704.png","rarity":1,"type_id":4,"sale_price_change_last_hour":0,"max_offer_unit_price":0,"data_id":65577,"price_last_changed":"2015-04-20 20:23:48 UTC","offer_price_change_last_hour":0,"name":"Twilight Arbor Token Loot Box","min_sale_unit_price":0,"restriction_level":0,"sub_type_id":0},{"sale_availability":0,"offer_availability":0,"img":"https:\/\/render.guildwars2.com\/file\/2626184EDDC254B4F7634A04F878062C6B2AF20D\/780372.png","rarity":1,"type_id":4,"sale_price_change_last_hour":0,"max_offer_unit_price":0,"data_id":65579,"price_last_changed":"2015-04-20 20:23:48 UTC","offer_price_change_last_hour":0,"name":"Twilight Arbor Weapons Box","min_sale_unit_price":0,"restriction_level":0,"sub_type_id":0}],"count":6,"page":1}

在挖掘解决方案并检查实际错误时,它表示期望和数组但是给出了一个字符串Log.i("gw2Log", resultClass.name[i]);

当我遍历此日志调用Log.i("gw2Log", resultClass.name);时,它会显示最后一个对象名称属性它循环的数量。

编辑: 根据请求包含我的resultClass.java:

public class resultClass {

    public int data_id;
    public String name;
    public int rarity;
    public int restriction_level;
    public String img;
    public int type_id;
    public int sub_type_id;
    public String price_last_changed;
    public int max_offer_unit_price;
    public int min_sale_unit_price;
    public int offer_availability;
    public int sale_availability;
    public int sale_price_change_last_hour;
    public int offer_price_change_last_hour;

    public int count;
    public int page;
    public int last_page;
    public int total;

    public int getData_id() {
        return data_id;
    }

    public void setData_id(int data_id) {
        this.data_id = data_id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getRarity() {
        return rarity;
    }

    public void setRarity(int rarity) {
        this.rarity = rarity;
    }

    public int getRestriction_level() {
        return restriction_level;
    }

    public void setRestriction_level(int restriction_level) {
        this.restriction_level = restriction_level;
    }

    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }

    public int getType_id() {
        return type_id;
    }

    public void setType_id(int type_id) {
        this.type_id = type_id;
    }

    public int getSub_type_id() {
        return sub_type_id;
    }

    public void setSub_type_id(int sub_type_id) {
        this.sub_type_id = sub_type_id;
    }

    public String getPrice_last_changed() {
        return price_last_changed;
    }

    public void setPrice_last_changed(String price_last_changed) {
        this.price_last_changed = price_last_changed;
    }

    public int getMax_offer_unit_price() {
        return max_offer_unit_price;
    }

    public void setMax_offer_unit_price(int max_offer_unit_price) {
        this.max_offer_unit_price = max_offer_unit_price;
    }

    public int getMin_sale_unit_price() {
        return min_sale_unit_price;
    }

    public void setMin_sale_unit_price(int min_sale_unit_price) {
        this.min_sale_unit_price = min_sale_unit_price;
    }

    public int getOffer_availability() {
        return offer_availability;
    }

    public void setOffer_availability(int offer_availability) {
        this.offer_availability = offer_availability;
    }

    public int getSale_availability() {
        return sale_availability;
    }

    public void setSale_availability(int sale_availability) {
        this.sale_availability = sale_availability;
    }

    public int getSale_price_change_last_hour() {
        return sale_price_change_last_hour;
    }

    public void setSale_price_change_last_hour(int sale_price_change_last_hour) {
        this.sale_price_change_last_hour = sale_price_change_last_hour;
    }

    public int getOffer_price_change_last_hour() {
        return offer_price_change_last_hour;
    }

    public void setOffer_price_change_last_hour(int offer_price_change_last_hour) {
        this.offer_price_change_last_hour = offer_price_change_last_hour;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getLast_page() {
        return last_page;
    }

    public void setLast_page(int last_page) {
        this.last_page = last_page;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }
}

3 个答案:

答案 0 :(得分:2)

尝试这样的事情

首先创建一个类似MyObject的新对象

public class MyObject {

public int data_id;
public String name;
public int rarity;
public int restriction_level;
public String img;
public int type_id;
public int sub_type_id;
public String price_last_changed;
public int max_offer_unit_price;
public int min_sale_unit_price;
public int offer_availability;
public int sale_availability;
public int sale_price_change_last_hour;
public int offer_price_change_last_hour;

public int getData_id() {
    return data_id;
}

public void setData_id(int data_id) {
    this.data_id = data_id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getRarity() {
    return rarity;
}

public void setRarity(int rarity) {
    this.rarity = rarity;
}

public int getRestriction_level() {
    return restriction_level;
}

public void setRestriction_level(int restriction_level) {
    this.restriction_level = restriction_level;
}

public String getImg() {
    return img;
}

public void setImg(String img) {
    this.img = img;
}

public int getType_id() {
    return type_id;
}

public void setType_id(int type_id) {
    this.type_id = type_id;
}

public int getSub_type_id() {
    return sub_type_id;
}

public void setSub_type_id(int sub_type_id) {
    this.sub_type_id = sub_type_id;
}

public String getPrice_last_changed() {
    return price_last_changed;
}

public void setPrice_last_changed(String price_last_changed) {
    this.price_last_changed = price_last_changed;
}

public int getMax_offer_unit_price() {
    return max_offer_unit_price;
}

public void setMax_offer_unit_price(int max_offer_unit_price) {
    this.max_offer_unit_price = max_offer_unit_price;
}

public int getMin_sale_unit_price() {
    return min_sale_unit_price;
}

public void setMin_sale_unit_price(int min_sale_unit_price) {
    this.min_sale_unit_price = min_sale_unit_price;
}

public int getOffer_availability() {
    return offer_availability;
}

public void setOffer_availability(int offer_availability) {
    this.offer_availability = offer_availability;
}

public int getSale_availability() {
    return sale_availability;
}

public void setSale_availability(int sale_availability) {
    this.sale_availability = sale_availability;
}

public int getSale_price_change_last_hour() {
    return sale_price_change_last_hour;
}

public void setSale_price_change_last_hour(int sale_price_change_last_hour) {
    this.sale_price_change_last_hour = sale_price_change_last_hour;
}

public int getOffer_price_change_last_hour() {
    return offer_price_change_last_hour;
}

public void setOffer_price_change_last_hour(int offer_price_change_last_hour) {
    this.offer_price_change_last_hour = offer_price_change_last_hour;
}
}

然后让你的resultClass看起来像这样

public class resultClass {

public int count;
public int page;
public int last_page;
public int total;
public ArrayList<MyObject> myObjects = new ArrayList();

public int getCount() {
    return count;
}

public void setCount(int count) {
    this.count = count;
}

public int getPage() {
    return page;
}

public void setPage(int page) {
    this.page = page;
}

public int getLast_page() {
    return last_page;
}

public void setLast_page(int last_page) {
    this.last_page = last_page;
}

public int getTotal() {
    return total;
}

public void setTotal(int total) {
    this.total = total;
}

public MyObject getObject(int pos){
    return myObjects.get(pos);
}
public void addObject(MyObject object)
{
    myObjects.add(object);
}
}

然后你的回答应该是这样的

public void onResponse(JSONObject object) {
    Log.i("gw2Log", "Json Response" + object);

    resultClass resultClass = new resultClass();

    try {
        resultClass.setCount(object.getInt("count"));
        resultClass.setPage(object.getInt("page"));
        resultClass.setLast_page(object.getInt("last_page"));
        resultClass.setTotal(object.getInt("total"));
        JSONArray list = new JSONArray(object.getString("results"));

        for (int i = 0; i < resultClass.getTotal(); i++) {
            JSONObject resultsObject = list.getJSONObject(i);
            MyObject temp = new MyObject();
            temp.setData_id(resultsObject
                    .getInt("data_id"));
            temp.setName(resultsObject
                    .getString("name"));
            temp.setRarity(resultsObject
                    .getInt("rarity"));
            temp.setRestriction_level(resultsObject
                    .getInt("restriction_level"));
            temp.setImg(resultsObject
                    .getString("img"));
            temp.setType_id(resultsObject
                    .getInt("type_id"));
            temp.setSub_type_id(resultsObject
                    .getInt("sub_type_id"));
            temp.setPrice_last_changed(resultsObject
                    .getString("price_last_changed"));
            temp.setMax_offer_unit_price(resultsObject
                    .getInt("max_offer_unit_price"));
            temp.setMin_sale_unit_price(resultsObject
                    .getInt("min_sale_unit_price"));
            temp.setOffer_availability(resultsObject
                    .getInt("offer_availability"));
            temp.setSale_availability(resultsObject
                    .getInt("sale_availability"));
            temp.setSale_price_change_last_hour(resultsObject
                    .getInt("sale_price_change_last_hour"));
            temp.setOffer_price_change_last_hour(resultsObject
                    .getInt("offer_price_change_last_hour"));
            resultClass.addObject(temp);
        }

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    for(int i = 0; i < resultClass.total; i++) {
        Log.i("gw2Log", resultClass.getObject(i).name);
    }
}

答案 1 :(得分:1)

你可以试试这段代码。您想使用对象表,但您只创建了单个对象。创建resultClass的arrayList,然后使用更简单的&#39; for&#39;为您的清单

public void onResponse(JSONObject object) {
    Log.i("gw2Log", "Json Response" + object);

    List<resultClass> resultClassList = new ArrayList<resultClass>();
    resultClass resultClass = new resultClass();

    try {
        resultClass.setCount(object.getInt("count"));
        resultClass.setPage(object.getInt("page"));
        resultClass.setLast_page(object.getInt("last_page"));
        resultClass.setTotal(object.getInt("total"));
        JSONArray list = new JSONArray(object.getString("results"));

        for (int i = 0; i < resultClass.getTotal(); i++) {
            JSONObject resultsObject = list.getJSONObject(i);

            resultClass.setData_id(resultsObject
                    .getInt("data_id"));
            resultClass.setName(resultsObject
                    .getString("name"));
            resultClass.setRarity(resultsObject
                    .getInt("rarity"));
            resultClass.setRestriction_level(resultsObject
                    .getInt("restriction_level"));
            resultClass.setImg(resultsObject
                    .getString("img"));
            resultClass.setType_id(resultsObject
                    .getInt("type_id"));
            resultClass.setSub_type_id(resultsObject
                    .getInt("sub_type_id"));
            resultClass.setPrice_last_changed(resultsObject
                    .getString("price_last_changed"));
            resultClass.setMax_offer_unit_price(resultsObject
                    .getInt("max_offer_unit_price"));
            resultClass.setMin_sale_unit_price(resultsObject
                    .getInt("min_sale_unit_price"));
            resultClass.setOffer_availability(resultsObject
                    .getInt("offer_availability"));
            resultClass.setSale_availability(resultsObject
                    .getInt("sale_availability"));
            resultClass.setSale_price_change_last_hour(resultsObject
                    .getInt("sale_price_change_last_hour"));
            resultClass.setOffer_price_change_last_hour(resultsObject
                    .getInt("offer_price_change_last_hour"));
            resultClassList.add(resultClass);
        }

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    for(resultClass result : resultClassList) {
        Log.i("gw2Log", result.name);
    }
}

答案 2 :(得分:0)

好的,我很确定几件事:

Tomer Shemesh是对的,你在每次迭代时都会覆盖你的resultClass对象。

您想在resultClass中使用常规变量制作数组,这样您实际上可以存储多个项目。

关于你得到的错误:

它出现在较低的for循环中,你要记录名称,对吗?

resultClass.name是一个String变量,它只包含一个字符串。您试图通过说resultClass.name[i]之类的内容从数组中获取字符串。为了工作你需要将它作为一个字符串数组decrlare,持有几个像public String[] name;的字符串,并在构造函数中使用给定的容量name = new String[cap];初始化它。或者你使用ArrayList<String>代替,它会动态增长,但使用自己的getter和setter而不是数组的简单名称[index]函数。

很抱歉,但我真的不想在这里编写正确的代码,因为几乎整个事情需要重写,我希望这会帮助你自己重写它。