jackson格式化后的字符串没有规律,以下直接将jackson格式化后的字符串解析后存入文本
//格式化json字符串public static String formatJson(String jsonStr) { if (null == jsonStr || "".equals(jsonStr)) return ""; StringBuilder sb = new StringBuilder(); char last = '\0'; char current = '\0'; int indent = 0; for (int i = 0; i < jsonStr.length(); i++) { last = current; current = jsonStr.charAt(i); switch (current) { case '{': case '[': sb.append(current); sb.append('\n'); indent++; addIndentBlank(sb, indent); break; case '}': case ']': sb.append('\n'); indent--; addIndentBlank(sb, indent); sb.append(current); break; case ',': sb.append(current); if (last != '\\') { sb.append('\n'); addIndentBlank(sb, indent); } break; default: sb.append(current); } } return sb.toString(); } /** * 添加space * @param sb * @param indent * @author xuhy * @Date 2015-10-14 上午10:38:04 */ public static void addIndentBlank(StringBuilder sb, int indent) { for (int i = 0; i < indent; i++) { sb.append('\t'); } } //直接用字符流写入文本了. str表示已经通过上面方法格式化后的字符串 public static void writeFile(String str,File file){ String b=formatJson(str); try { // 创建文件对象 //File fileText = new File("D:\\Users\\Administrator\\Desktop\\index\\tfjbp-features-sysEN.json"); // 向文件写入对象写入信息 FileWriter fw = new FileWriter(file); BufferedWriter bw=new BufferedWriter(fw); // 写文件 bw.write(b); bw.flush();//强制输出下免得 en写入数据不完整 // 关闭 bw.close(); fw.close(); } catch (IOException e) { // e.printStackTrace(); } }
效果如下:
格式化前
{"userInfo.birthday":"Birthday","report.img":"","report.company":"","userInfo.address":"Address","newPassword":"","deptIsEnabled":"","updateUser.tel":""}
格式化后
{
"userInfo.birthday":"Birthday", "report.img":"", "report.company":"", "userInfo.address":"Address", "newPassword":"", "deptIsEnabled":"", "updateUser.tel":"" }