Java中List集合和JSON对象之间的相互转换
第一种方法: 代码实现 /** *数据封装成json * * @param items 物料入库数据 * @return json * @throws JSONException */ public static String GoodIn2Json(List<GoodInfo> items) throws JSONException { if (items == null) return ""; JSONArray array = new JSONArray(); JSONObject jsonObject = null; GoodInfo info = null; for (int i = 0; i < items.size(); i++) { info = items.get(i); jsonObject = new JSONObject(); jsonObject.put(Api.COLORID, info.getColorId()); jsonObject.put(Api.STOCK, info.getStock()); array.put(jsonObject); } return array.toString(); } /** * 将json数组解析出来,生成自定义数据的数组 * @param data 包含用户自定义数据的json * @return