1. 程式人生 > >JSON和List相互轉化

JSON和List相互轉化

List<Goods> goodsList = new ArrayList<>();

goodsList.add(good);

生成JSONArray

JSONArray array = JSONArray.fromObject(goodsList);

params.put("goodsList", array.toString());

解析

List<Goods> goodsList = new ArrayList<>();

JSONArray jsonArray = JSONArray.fromObject(params.get("goodsList"));

goodsList = JSONArray.toList(jsonArray,Goods.class);//這裡的tClass<T>