我正在使用ArrayList<HashMap<String,String>>
来存储我的购物车商品。但我需要将其转换为JSONArray
以将其发送到数据库。但是,当我将其转换为JSONArray
时,JSONArray
看起来像这样:
03-13 11:09:28.842:D / cart之前(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:转换成JSONArray后的D / cart(1339): [&#34; {image = 2130837526,category =椅子,数量= 1,价格= 400, name = chair,prodId = 34}&#34;,&#34; {image = 2130837566,category = Mirrors, 数量= 1,价格= 3000,名称= La Fonda,prodId = 35}&#34;]
我认为这是错误的。相反,它应该转换为这样的东西:
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"}]
要转换为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;
}
持有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);
}
}
答案 0 :(得分:0)
我发现这对我的项目有帮助:
我的ArrayList中的对象&lt;&gt;
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;
}
}
实际转换:
ArrayList<ListItem> myCustomList = ArrayList<ListItem>();
JSONArray jsonArray = new JSONArray();
for (int i=0; i < myCustomList.size(); i++) {
jsonArray.put(myCustomList.get(i).getJSONObject());
}