ArrayList<ShenPin> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
list.add(new ShenPin("商品" + i, 20 + i, R.mipmap.ccc));
}
JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = new JSONArray();
for (ShenPin sp : list) {
JSONObject jsonObject1 = new JSONObject();
try {
jsonObject1.put("name", sp.getName());
jsonObject1.put("jiage", sp.getAge());
jsonObject1.put("img", sp.getImg());
} catch (JSONException e) {
e.printStackTrace();
}
jsonArray.put(jsonObject1);
}
try {
jsonObject.put("shengpin", jsonArray);
} catch (JSONException e) {
e.printStackTrace();
}