在Java中从ArrayList中查找缺少的值

在Java中从ArrayList中查找缺少的值,java,android,arraylist,pojo,Java,Android,Arraylist,Pojo,我有一个问题,ArrayList中的值丢失了。当我toastlistCartItemValues.toString()时,结果与现有数据一致。但是当我在DataItemCart模型中设置listCartItemValues时,我发现缺少一个值,那就是星光 我的API: { "data": [ { "cart_item_values": [ { "attribute_name":

我有一个问题,ArrayList中的值丢失了。当我toast
listCartItemValues.toString()
时,结果与现有数据一致。但是当我在DataItemCart模型中设置
listCartItemValues
时,我发现缺少一个值,那就是星光

我的API:

{
    "data": [
        {
            "cart_item_values": [
                {
                    "attribute_name": "color",
                    "attribute_value": "Starlight"
                },
                {
                    "attribute_name": "size",
                    "attribute_value": "XL"
                }
            ]
        },
        {
            "cart_item_values": [
                {
                    "attribute_name": "size",
                    "attribute_value": "10"
                }
            ]
        }
    ],
}
获取购物车项目值的我的代码:

private List<CartItemValuesItem> listCartItemValues;

for (int a = 0; a < data.length(); a++) {

    JSONObject dataJSONObject = data.getJSONObject(a);

    final DataItemCart dataItemCart = new DataItemCart();
    final CartItemValuesItem cartItemValuesItem = new CartItemValuesItem();

    JSONArray arrayCartItemValues = dataJSONObject.optJSONArray("cart_item_values");

    for (int b = 0; b < arrayCartItemValues.length(); b++) {
        JSONObject cartItemJsonObject = arrayCartItemValues.optJSONObject(b);
        listCartItemValues = new ArrayList<>();

        cartItemValuesItem.setAttributeName(cartItemJsonObject.getString("attribute_name"));
        cartItemValuesItem.setAttributeValue(cartItemJsonObject.getString("attribute_value"));
        listCartItemValues.add(cartItemValuesItem);

        Toast.makeText(mContext, "value : "+ listCartItemValues.toString(), Toast.LENGTH_SHORT).show();
        // this list contain Starlight, XL, and 10
    }

    dataItemCart.setCartItemValues(listCartItemValues);

    Toast.makeText(mContext, "data : "+ dataItemCart.getCartItemValues.toString(), Toast.LENGTH_SHORT).show();
    // but this list contain XL and 10. Starlight is missing
}
私有列表ListCartItemValue;
对于(int a=0;a
如何从ArrayList中查找缺少的值并将缺少的值输入到
dataItemCart.setCartItemValues

移动“final dataItemCart dataItemCart=new dataItemCart();”第一个For循环之外

将listCartItemValues=new ArrayList();“移到第二个For循环之外

这是修改过的代码

private List<CartItemValuesItem> listCartItemValues;
    final DataItemCart dataItemCart = new DataItemCart();
    for (int a = 0; a < data.length(); a++) {

        JSONObject dataJSONObject = data.getJSONObject(a);

        final CartItemValuesItem cartItemValuesItem = new CartItemValuesItem();

        JSONArray arrayCartItemValues = dataJSONObject.optJSONArray("cart_item_values");
        listCartItemValues = new ArrayList<>();

        for (int b = 0; b < arrayCartItemValues.length(); b++) {
            JSONObject cartItemJsonObject = arrayCartItemValues.optJSONObject(b);
            cartItemValuesItem.setAttributeName(cartItemJsonObject.getString("attribute_name"));
            cartItemValuesItem.setAttributeValue(cartItemJsonObject.getString("attribute_value"));
            listCartItemValues.add(cartItemValuesItem);

            Toast.makeText(mContext, "value : "+ listCartItemValues.toString(), Toast.LENGTH_SHORT).show();
            // this list contain Starlight, XL, and 10
        }

        dataItemCart.setCartItemValues(listCartItemValues);

        Toast.makeText(mContext, "data : "+ dataItemCart.getCartItemValues.toString(), Toast.LENGTH_SHORT).show();
        // but this list contain XL and 10. Starlight is missing
    }
私有列表ListCartItemValue;
final DataItemCart DataItemCart=新DataItemCart();
对于(int a=0;a
Move“final-DataItemCart-DataItemCart=new-DataItemCart();”在第一个For循环之外

将listCartItemValues=new ArrayList();“移到第二个For循环之外

这是修改过的代码

private List<CartItemValuesItem> listCartItemValues;
    final DataItemCart dataItemCart = new DataItemCart();
    for (int a = 0; a < data.length(); a++) {

        JSONObject dataJSONObject = data.getJSONObject(a);

        final CartItemValuesItem cartItemValuesItem = new CartItemValuesItem();

        JSONArray arrayCartItemValues = dataJSONObject.optJSONArray("cart_item_values");
        listCartItemValues = new ArrayList<>();

        for (int b = 0; b < arrayCartItemValues.length(); b++) {
            JSONObject cartItemJsonObject = arrayCartItemValues.optJSONObject(b);
            cartItemValuesItem.setAttributeName(cartItemJsonObject.getString("attribute_name"));
            cartItemValuesItem.setAttributeValue(cartItemJsonObject.getString("attribute_value"));
            listCartItemValues.add(cartItemValuesItem);

            Toast.makeText(mContext, "value : "+ listCartItemValues.toString(), Toast.LENGTH_SHORT).show();
            // this list contain Starlight, XL, and 10
        }

        dataItemCart.setCartItemValues(listCartItemValues);

        Toast.makeText(mContext, "data : "+ dataItemCart.getCartItemValues.toString(), Toast.LENGTH_SHORT).show();
        // but this list contain XL and 10. Starlight is missing
    }
私有列表ListCartItemValue;
final DataItemCart DataItemCart=新DataItemCart();
对于(int a=0;a
为什么不将json转换为map,然后执行操作?为什么不将json转换为map,然后执行操作?谢谢您的回答,但您的代码对我不起作用。我toast
dataItemCart.getCartItemValues
只得到10。星光和XL正在丢失你的答案,但你的代码对我不起作用。我toast
dataItemCart.getCartItemValues
只得到10。星光和XL不见了