public String friendlyText(List data) { if(CollectionUtils.isEmpty(data)) { return "[]"; } StringJoiner joiner = new StringJoiner(",", "[", "]"); String body = (String)data.stream().map(entry -> toJsonString(entry)).collect(Collectors.joining(",")); return joiner.add(body).toString(); } public String toJsonString(Object target) { return JSON.toJSONStringWithDateFormat(target,"yyyy-MM-dd HH:mm:ss"); } List data = new ArrayList(); for (int i = 0; i <10 ; i++) { SnmpMemory memory = new SnmpMemory .Builder() .withId(Long.valueOf(i)) .withCreateTime(LocalDateTime.now()) .withMemTotalReal(1024L) .withMemTotalSwap(512L) .withNodeId(Long.valueOf(i^2)) .build(); data.add(memory); } String friendlyText = (String)data.stream().reduce(new StringBuilder(), (buffer, item) -> buffer.append(toJsonString(item)).append(','), StringBuffer::append).toString(); String body = (String)data.stream().map(entry -> toJsonString(entry)).collect(Collectors.joining(", ")); String friendlyText = "[".concat(body).concat("]"); System.out.println(friendlyText); StringJoiner joiner = new StringJoiner(",", "[", "]"); String body = (String)data.stream().map(entry -> toJsonString(entry)).collect(Collectors.joining(",")); String friendlyText = joiner.add(body).toString(); System.out.println(friendlyText); 输出结果: [{"createTime":"2019-08-09 11:20:06","id":0,"memTotalReal":1024,"memTotalSwap":512,"nodeId":2},{"createTime":"2019-08-09 11:20:06","id":1,"memTotalReal":1024,"memTotalSwap":512,"nodeId":3},{"createTime":"2019-08-09 11:20:06","id":2,"memTotalReal":1024,"memTotalSwap":512,"nodeId":0},{"createTime":"2019-08-09 11:20:06","id":3,"memTotalReal":1024,"memTotalSwap":512,"nodeId":1},{"createTime":"2019-08-09 11:20:06","id":4,"memTotalReal":1024,"memTotalSwap":512,"nodeId":6},{"createTime":"2019-08-09 11:20:06","id":5,"memTotalReal":1024,"memTotalSwap":512,"nodeId":7},{"createTime":"2019-08-09 11:20:06","id":6,"memTotalReal":1024,"memTotalSwap":512,"nodeId":4},{"createTime":"2019-08-09 11:20:06","id":7,"memTotalReal":1024,"memTotalSwap":512,"nodeId":5},{"createTime":"2019-08-09 11:20:06","id":8,"memTotalReal":1024,"memTotalSwap":512,"nodeId":10},{"createTime":"2019-08-09 11:20:06","id":9,"memTotalReal":1024,"memTotalSwap":512,"nodeId":11}] String friendlyText = (String)data.stream().reduce(new StringBuilder(), (buffer, item) -> buffer.append(toJsonString(item)).append(','), StringBuffer::append).toString(); 输出结果: {"createTime":"2019-08-09 11:20:06","id":0,"memTotalReal":1024,"memTotalSwap":512,"nodeId":2},{"createTime":"2019-08-09 11:20:06","id":1,"memTotalReal":1024,"memTotalSwap":512,"nodeId":3},{"createTime":"2019-08-09 11:20:06","id":2,"memTotalReal":1024,"memTotalSwap":512,"nodeId":0},{"createTime":"2019-08-09 11:20:06","id":3,"memTotalReal":1024,"memTotalSwap":512,"nodeId":1},{"createTime":"2019-08-09 11:20:06","id":4,"memTotalReal":1024,"memTotalSwap":512,"nodeId":6},{"createTime":"2019-08-09 11:20:06","id":5,"memTotalReal":1024,"memTotalSwap":512,"nodeId":7},{"createTime":"2019-08-09 11:20:06","id":6,"memTotalReal":1024,"memTotalSwap":512,"nodeId":4},{"createTime":"2019-08-09 11:20:06","id":7,"memTotalReal":1024,"memTotalSwap":512,"nodeId":5},{"createTime":"2019-08-09 11:20:06","id":8,"memTotalReal":1024,"memTotalSwap":512,"nodeId":10},{"createTime":"2019-08-09 11:20:06","id":9,"memTotalReal":1024,"memTotalSwap":512,"nodeId":11}
本博客文章绝大多数为原创,少量为转载,代码经过测试验证,如果有疑问直接留言或者私信我。
创作文章不容易,转载文章必须注明文章出处;如果这篇文章对您有帮助,点击右侧打赏,支持一下吧。
创作文章不容易,转载文章必须注明文章出处;如果这篇文章对您有帮助,点击右侧打赏,支持一下吧。