首页 > 代码库 > JSON格式化着色小工具
JSON格式化着色小工具
在我们系统开发过程中越来越多的使用JSON作为数据交换的载体。JSON的特点是层次结构简洁清晰,易于人阅读和编写。同时也易于机器解析和生成。对于机器解析和生成这点本人表示认可,然而对于易于人阅读和编写可以通过下面一段JSON字符串来验证下:
{"strValue":"here is str value","nullValue":null,"intvalue":999,"doublevalue":999,"booleanValue":true,"array":["a1","a2",true,2,33.3,null,{"innerStr":"here is a inner str","innerInteger":123456789},["Hi, found me ?"]],"innerOBJ":{"innerStr":"here is a inner str","innerInteger":123456789}}
通过上面的字符串可以看到系统间交换数据的JSON字符串通常是去除了格式的,这样的JSON我们阅读起来貌似并不是那么轻松。在网上有很多JSON格式和着色的插件,借助这些插件可以将JSON字符串进行格式化,这时候JSON的易于人阅读才表现出来。
{ "strValue": "here is str value", "nullValue": null, "intvalue": 999, "doublevalue": 999, "booleanValue": true, "array": [ "a1", "a2", true, 2, 33.3, null, { "innerStr": "here is a inner str", "innerInteger": 123456789 }, [ "Hi, found me ?" ] ], "innerOBJ": { "innerStr": "here is a inner str", "innerInteger": 123456789 } }
网上有很多JSON格式和着色的插件,找了一圈都是JS编写的,没有发现C/S的小工具。于是偶尔强迫症发作决定整一个小工具,下面直接上图:
主要的业务代码即为下面的工具类,提供了JSON校验、格式化、着色、删除空格、删除空格并转义、去除转义等功能。
import java.io.FileReader; import java.io.IOException; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.script.Invocable; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; /** * JSON格式化工具 * @author StarlightFu * */ public class JsonFormatUtil { private static final String OBJECT_BRACE_REG = "([\\{\\}])"; private static final String ARRAY_BRACE_REG = "([\\[\\]])"; private static final String PROPERTY_NAME_REG = "(\\\".*\\\")(\\:)(.*)(\\,)?"; private static final String STRING_REG = "\\\"([^\"]*)\\\"(\\,)?$"; private static final String NUMBER_REG = "(-?\\d+)(\\,)?$"; /** JSON着色样式 */ public static final String JSON_STYLE = "<style>pre{font-family:\"微软雅黑\";font-size:10px;}.ObjectBrace{color:#00AA00;font-weight:bold;}.ArrayBrace{color:#0033FF;font-weight:bold;}" + ".PropertyName{color:#CC0000;font-weight:bold;}.String{color:#007777;}.Number{color:#AA00AA;}.Boolean{color:#0000FF;}" + ".Function{color:#AA6633;text-decoration:italic;}.Null{color:#0000FF;}.Comma{color:#000000;font-weight:bold;</style>"; /** * 校验JSON格式是否正确 * @param json JSON字符串 * @return true:格式正确;false:格式错误 */ public static boolean validation(String json){ boolean result = false; FileReader reader = null; try { String valJson = "function valJson(jsonStr){var result=false;try{eval(‘(‘+jsonStr+‘)‘);result=true;}catch(e){result=false;}return result;}"; ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("javascript"); engine.eval(valJson); if (engine instanceof Invocable) { Invocable invoke = (Invocable) engine; result = (Boolean) invoke.invokeFunction("valJson", json); } } catch (ScriptException e) { throw new ServiceException("执行JSON校验JS异常"); } catch (NoSuchMethodException e) { throw new ServiceException("执行JSON校验JS异常"); } finally { try { if (reader != null) { reader.close(); } } catch (IOException e) { e.printStackTrace(); } } return result; } /** * 格式化JSON字符串 * @param jsonStr * @return * @throws ServiceException */ public static String format(String jsonStr)throws ServiceException{ boolean valResult = false; valResult = JsonFormatUtil.validation(jsonStr); if (!valResult) { throw new ServiceException("JSON格式错误"); } if (null == jsonStr || "".equals(jsonStr)) return ""; StringBuilder sb = new StringBuilder(); char last = ‘\0‘; char current = ‘\0‘; int indent = 0; boolean inString = false; char inStringBegin = ‘\0‘; for (int i = 0; i < jsonStr.length(); i++) { last = current; current = jsonStr.charAt(i); if (inString && current == inStringBegin) { // 判断前一个字符是否为 \ if (last != ‘\\‘) { inString = false; inStringBegin = ‘\0‘; } sb.append(current); } else if (!inString && (current == ‘"‘ || current == ‘\‘‘)) { inString = true; inStringBegin = current; sb.append(current); } else if (!inString && (current == ‘ ‘ || current == ‘\t‘ || current == ‘\n‘)) { current = ‘\0‘; } else if (!inString && current == ‘:‘) { sb.append(current).append(" "); } else if (!inString && current == ‘,‘) { sb.append(current).append(‘\n‘).append(indentBlank(indent)); } else if (!inString && (current == ‘[‘ || current == ‘{‘)) { indent++; sb.append(current).append(‘\n‘).append(indentBlank(indent)); } else if (!inString && (current == ‘]‘ || current == ‘}‘)) { indent--; sb.append(‘\n‘).append(indentBlank(indent)).append(current); } else { sb.append(current); } } return sb.toString(); } /** * 添加缩进 * @param indent * @return */ private static String indentBlank(int indent) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < indent; i++) { sb.append(" "); } return sb.toString(); } /** * JSON语法着色 * @param formatJsonStr 格式化后的JSON字符串 * @return */ public static String coloring(String formatJsonStr) { StringBuilder sb = new StringBuilder(); String[] nodes = formatJsonStr.split("\n"); for (int i = 0; i < nodes.length; i++) { String node = nodes[i]; Pattern r = Pattern.compile(JsonFormatUtil.OBJECT_BRACE_REG); Matcher m = r.matcher(node); node = m.replaceAll("<span class=‘ObjectBrace‘>$1</span>"); r = Pattern.compile(JsonFormatUtil.ARRAY_BRACE_REG); m = r.matcher(node); node = m.replaceAll("<span class=‘ArrayBrace‘>$1</span>"); r = Pattern.compile(JsonFormatUtil.PROPERTY_NAME_REG); m = r.matcher(node); node = m.replaceAll("<span class=‘PropertyName‘>$1</span>$2$3$4"); r = Pattern.compile(JsonFormatUtil.STRING_REG); m = r.matcher(node); node = m.replaceAll("<span class=‘String‘>\"$1\"</span><span class=‘Comma‘>$2</span>"); r = Pattern.compile(JsonFormatUtil.NUMBER_REG); m = r.matcher(node); node = m.replaceAll("<span class=‘Number‘>$1</span><span class=‘Comma‘>$2</span>"); sb.append(node); if (i < nodes.length - 1) { sb.append("\n"); } } return sb.toString(); } /** * 格式化JSON并着色 * @param json * @return */ public static String formatAndColoring(String json){ return JsonFormatUtil.coloring(JsonFormatUtil.format(json)); } /** * 删除空格 * @param jsonStr * @return */ public static String removeSpace(String jsonStr){ StringBuilder sb = new StringBuilder(); char last = ‘\0‘; char current = ‘\0‘; char inStringBegin = ‘\0‘; boolean inString = false; for (int i = 0; i < jsonStr.length(); i++) { last = current; current = jsonStr.charAt(i); if (inString && current == inStringBegin) { // 判断前一个字符是否为 \ if (last != ‘\\‘) { inString = false; inStringBegin = ‘\0‘; } sb.append(current); } else if (!inString && (current == ‘"‘ || current == ‘\‘‘)) { inString = true; inStringBegin = current; sb.append(current); } else if (!inString && (current == ‘ ‘ || current == ‘\t‘ || current == ‘\n‘)) { current = ‘\0‘; }else{ sb.append(current); } } return sb.toString(); } /** * 删除空格并转义 * @param jsonStr * @return */ public static String removeSpaceAndEscape(String jsonStr){ jsonStr = JsonFormatUtil.removeSpace(jsonStr); return jsonStr.replaceAll("\"", "\\\\\""); } /** * 去除转义 * @param jsonStr * @return */ public static String removeEscape(String jsonStr){ return jsonStr.replaceAll("\\\\\"", "\""); } public static void main(String[] args) { String jsonStr = "{\"strValue\":\"here is str value\",\"nullValue\":null,\"intvalue\":999,\"doublevalue\":999,\"booleanValue\":true,\"array\":[\"a1\",\"a2\",true,2,33.3,null,{\"innerStr\":\"here is a inner str\",\"innerInteger\":123456789},[\"Hi, found me ?\"]],\"innerOBJ\":{\"innerStr\":\"here is a inner str\",\"innerInteger\":123456789}}"; System.out.println(JsonFormatUtil.format(jsonStr)); System.out.println(JsonFormatUtil.coloring(JsonFormatUtil.format(jsonStr))); } }
下面分享下基于上述代码实现的桌面小应用,有兴趣的同学可以下载,URL:JSON格式化小工具
JSON格式化着色小工具
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。