1. 程式人生 > >Java中json轉list,list轉陣列

Java中json轉list,list轉陣列

json轉list

    public static List<String> jsonToList(String s){
        List<String> list =new ArrayList<>();
        try {
            JSONArray jsonArray = JSONArray.fromObject(s);
            for (int i=0;i<jsonArray.size();i++){
                list.add((String) jsonArray.get(i));
            }
        } catch (JSONException e1) {
            e1.printStackTrace();
        }
        return list;
    }

list轉陣列

List<String> eqList = BaseUtils.jsonToList(announcement.getEqList());
			// 使用toArray(T[] a)方法
			String[] eqArr = eqList.toArray(new String[eqList.size()]);
			List<JSONObject> joList2 = new ArrayList<>();
			// 列印該陣列
			for (String eq : eqArr) {
				JSONObject jo2 = new JSONObject();
				EEquipmentManageEntity equipment = eEquipmentManageDao.selectById(eq);
				jo2.put("id", equipment.getId());
				jo2.put("name", equipment.getName());
				joList2.add(jo2);
			}