Object json=JSONObject.fromObject("{}"); List<Object> list = new ArrayList<Object>(); list.add("1"); list.add("2"); list.add("3"); list.add("4"); list.add("5"); list.add("6"); List<Object> result = new ArrayList<Object>(); Map<String, Object> map = new HashMap<String, Object>(); for(Object str :list){ map.put("name", str); map.put("age", str); result.add(map); } JSONArray arr=JSONArray.fromObject(result); if(arr!=null){ json=arr; } System.out.println(arr);
输出值格式为:[{"age":"6","name":"6"},{"age":"6","name":"6"},{"age":"6","name":"6"},{"age":"6","name":"6"},{"age":"6","name":"6"},{"age":"6","name":"6"}]