且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

转换的ArrayList到JSONArray

更新时间:2023-10-07 08:16:46

我觉得这是在我的项目有帮助的:

在对象的ArrayList我LT&;>

 公共类列表项{
    私人长期_masterId;
    私人字符串_name;
    私人长期_category;    公共列表项(长masterId,字符串名称,长类){
        _masterId = masterId;
        _name =名称;
        _category =类别;
    }    公众的JSONObject getJSONObject(){
        JSONObject的OBJ =新的JSONObject();
        尝试{
            obj.put(ID,_masterId);
            obj.put(名,_name);
            obj.put(类别,_category);
        }赶上(JSONException E){
            迹(DefaultListItem.toString JSONException:+ e.getMessage());
        }
        返回OBJ;
    }
}

实际的转换:

 的ArrayList<&列表项GT; myCustomList = ArrayList的<&列表项GT;();
JSONArray jsonArray =新JSONArray();
的for(int i = 0; I< myCustomList.size();我++){
        jsonArray.put(myCustomList.get(ⅰ).getJSONObject());
}

I am using ArrayList<HashMap<String,String>> to store my cart items. But I need to convert it to JSONArray to send it to the database. But when I convert it to JSONArray the JSONArray looks like this:

03-13 11:09:28.842: D/cart before(1339): [{image=2130837526, category=Chairs, Quantity=1, price=400, name=chair, prodId=34}, {image=2130837566, category=Mirrors, Quantity=1, price=3000, name=La Fonda, prodId=35}]

03-13 11:09:28.842: D/cart after converting into JSONArray(1339): ["{image=2130837526, category=Chairs, Quantity=1, price=400, name=chair, prodId=34}","{image=2130837566, category=Mirrors, Quantity=1, price=3000, name=La Fonda, prodId=35}"]

Which I believe is wrong. Instead it should be converted to something like this:

cartitems=[{"name":"Chair","price":"1001","prodId":"2","category":"Chairs","image":"2130837519","Quantity":"1"},{"name":"Baxton Studio Club Chair","price":"4545","prodId":"5","category":"Chairs","image":"2130837521","Quantity":"1"}]

Code to convert to JSONArray:

protected String doInBackground(String... args) {
    AddtoCart obj = (AddtoCart) getApplicationContext();
    JSONArray cart = new JSONArray(obj.getCart());
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("username", username);
    params.put("email", email);
    params.put("payment", payment);
    params.put("address", useraddress);
    params.put("contact", contact);
    params.put("city", usercity);
    params.put("cartitems", cart.toString());
    Log.d("params", params.toString());

    JSONObject json = jParser.makeHttpRequest(url_all_products, "POST", params);
    try {
        success = json.getInt("success");
        message = json.getString("message");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}

Class holding ArrayList:

public class AddtoCart extends Application {
    private static final String TAG_QUANTITY = "Quantity";
    private static final String TAG_PRICE = "price";
    ArrayList<HashMap<String, String>> cart = new ArrayList<HashMap<String, String>>();

    public void setCart(ArrayList<HashMap<String, String>> data) {
        //cart = data;
        cart.addAll(data);
        Log.d("Items in the cart", String.valueOf(cart));
    }

    public ArrayList<HashMap<String, String>> getCart() {
        return cart;
    }

    public int getSize() {
        return cart.size();
    }

    public void updateCart(ArrayList<HashMap<String, String>> data) {
        cart = data;
        Log.d("UPDATED CART", String.valueOf(cart));
    }

    public void updateQuantity(int index, String quantity) {
        cart.get(index).put(TAG_QUANTITY,quantity);
    }
}

I found this to be helpful in my project:

Object in my ArrayList<>

public class ListItem {
    private long _masterId;
    private String _name;
    private long _category;

    public ListItem(long masterId, String name, long category) {
        _masterId = masterId;
        _name = name;
        _category = category;
    }

    public JSONObject getJSONObject() {
        JSONObject obj = new JSONObject();
        try {
            obj.put("Id", _masterId);
            obj.put("Name", _name);
            obj.put("Category", _category);
        } catch (JSONException e) {
            trace("DefaultListItem.toString JSONException: "+e.getMessage());
        }
        return obj;
    }
}

The actual conversion:

ArrayList<ListItem> myCustomList = ArrayList<ListItem>();
JSONArray jsonArray = new JSONArray();
for (int i=0; i < myCustomList.size(); i++) {
        jsonArray.put(myCustomList.get(i).getJSONObject());
}