DDRJSONUtils { public static String toJSONString(Collection collection) { if (collection == null) { return null; } else { StringBuilder sb = new StringBuilder(); sb.append('['); for (Object item : collection) { if (item == null) { sb.append("null"); } else if (item instanceof String || item instanceof Character) { sb.append('\"'); sb.append(item); sb.append('\"'); } else if (item instanceof Map) { sb.append(toJSONString((Map) item)); } else { sb.append(item); } sb.append(','); } if (!collection.isEmpty()) { sb.deleteCharAt(sb.length() - 1); } sb.append(']'); return sb.toString(); } }  static String toJSONString(Collection collection); static String toJSONString(Map map);  }
@Test public void test01() { List<String> list = new ArrayList<>(); list.add("12"); list.add("34"); list.add(null); String src = DDRJSONUtils.toJSONString(list); Assert.equals(src, "[\"12\",\"34\",null]"); }
@Test public void test02() { List<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(null); String src = DDRJSONUtils.toJSONString(list); Assert.equals(src, "[1,2,null]"); }
@Test public void test03() { List list = new ArrayList(); list.add(1); list.add("2"); list.add(null); String src = DDRJSONUtils.toJSONString(list); Assert.equals(src, "[1,\"2\",null]"); }
@Test public void test04() { Map map = new LinkedHashMap<>(); map.put(1, "12"); map.put("2", 13); map.put("2", null); map.put(null, 14); String src = DDRJSONUtils.toJSONString(map); Assert.equals(src, "{1:\"12\",\"2\":null,null:14}"); }
@Test public void test05() { Map map = new LinkedHashMap<>(); map.put(1, "12"); map.put("2", 13); List list = new ArrayList(); list.add("78"); list.add(90); map.put(56, list); String src = DDRJSONUtils.toJSONString(map); Assert.equals(src, "{1:\"12\",\"2\":13,56:[\"78\",90]}"); }
@Test public void test06() { List list = new ArrayList(); list.add("78"); list.add(90); Map map = new LinkedHashMap<>(); map.put(1, "12"); map.put("2", 13); list.add(map); String src = DDRJSONUtils.toJSONString(list); Assert.equals(src, "[\"78\",90,{1:\"12\",\"2\":13}]"); }