一般的json文件拥有很多的空格和注释,虽然读起来比较方便,但是在运行的时候是要占一些内存的。

于是json压缩工具就应运而生了,这个工具是用java做的,原理是:

1:在Eclipse中导出一个可运行的jar文件

2:用python运行这个jar文件,并向这个jar文件的运行程序传一些main方法的参数(一般只传路径就可以了)

  1. //bat文件的代码如下:
  2. set assetsAPath="..\javaOutput\assets\uijson" json文件路径
  3. set assetsBPath="..\javaOutput\assets\uijson\\"  压缩后的json文件路径
  4. set targetJar="JSONMinify.jar"
  5. cd ./lang
  6. java -jar lib/JSONMinify.jar
  7. cd ..
  8. cd ./tools
  9. java -jar %targetJar% resVersion=%resVersion% assetsAPath=%assetsAPath% assetsBPath=%assetsBPath%
  10.  
  11. pause

  //java代码如下

  1. package com.pack;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.File;
  6. import java.io.FileReader;
  7. import java.io.FileWriter;
  8. import java.io.IOException;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11.  
  12. import com.zhaohe.download.ResourceDownloader;
  13.  
  14. public class JSONMinify {
  15.  
  16. static ArrayList<String> fileName;
  17.  
  18. public static void main(String[] args) throws Exception {
  19. initArgs(args);
  20. String fileData1 = assetsAPath;
  21. String fileDatas2 = assetsBPath;
  22.  
  23. fileName = new ArrayList<String>();
  24. File folder = new File(fileDatas2);
  25. List<String> filesPath = getAllFile(fileData1, false);
  26.  
  27. System.err.println(getAllFile(fileData1, false).size());
  28.  
  29. System.err.println(fileData1);
  30.  
  31. for (int i = 0; i < getAllFile(fileData1, false).size(); i++) {
  32. System.err.println(filesPath.get(i));
  33.  
  34. File file = new File(filesPath.get(i));// 需要读取的文件
  35. File file1 = new File(fileDatas2, fileName.get(i));// 需要写进去的文件
  36. String fileContext = txt2String(file);// 文件内容
  37. file.delete();
  38. if (!file1.exists()) {
  39. file.createNewFile();
  40. }
  41.              
  42.          PrintWriter out1 = new PrintWriter(new BuffewedWriter(new OutputStreamWriter(new FileOutputStream(fileDatas2+fileName.get(i)),"utf-8")));
  43.          out1.write(minify(fileContext));
  44.  
  45. out.write(minify(fileContext));
  46. out.close();
  47.   
  48.  
  49. }
  50. }
  51.  
  52. private static final String manifestFileName = "assetsManifest.txt";
  53. private static final String remoteVersionFile = "assetsVersion.txt";
  54. private static final String lcoalVersionFile = "localAssetsVersion.txt";
  55.  
  56. private static String version = "1.0.2";
  57. private static String assetsAPath = "..\\android\\assets\\uiname\\";
  58. private static String assetsBPath = "..\\android\\assets\\uijson\\";
  59. private static String rootFolder = ".\\packages";
  60. private static String cdnUrl = "http://www.mathmt.com";
  61. private static String serverID = "12";
  62. private static String resVersion = String.valueOf(ResourceDownloader.RES_TYPE_CURRENT);
  63.  
  64. public static void initArgs(String args[]) {
  65. if (args != null) {
  66. for (String arg : args) {
  67. String[] keyValue = arg.split("=");
  68. final String key = keyValue[0];
  69. final String value = keyValue[1];
  70. if ("targetVersion".equals(key)) {
  71. version = value;
  72. } else if ("assetsAPath".equals(key)) {
  73. assetsAPath = value;
  74. } else if ("assetsBPath".equals(key)) {
  75. assetsBPath = value;
  76. } else if ("cdnUrl".equals(key)) {
  77. cdnUrl = value;
  78. } else if ("rootFolder".equals(key)) {
  79. rootFolder = value;
  80. } else if ("serverID".equals(key)) {
  81. serverID = value;
  82. } else if ("resVersion".equals(key)) {
  83. resVersion = value;
  84. } else {
  85. System.out.println("Unknown key:" + key);
  86. }
  87. }
  88. }
  89. }
  90.  
  91. public static boolean deleteDir(String path) {
  92. File file = new File(path);
  93. if (!file.exists()) {// 判断是否待删除目录是否存在
  94. return false;
  95. }
  96.  
  97. String[] content = file.list();// 取得当前目录下所有文件和文件夹
  98. for (String name : content) {
  99. File temp = new File(path, name);
  100. if (temp.isDirectory()) {// 判断是否是目录
  101. deleteDir(temp.getAbsolutePath());// 递归调用,删除目录里的内容
  102. temp.delete();// 删除空目录
  103. } else {
  104. if (!temp.delete()) {// 直接删除文件
  105. temp.delete();
  106. System.err.println("Failed to delete " + name);
  107. }
  108. }
  109. }
  110. return true;
  111. }
  112.  
  113. /*
  114. * 读取txt文件的内容
  115. *
  116. * @param file 想要读取的文件对象
  117. *
  118. * @return 返回文件内容
  119. */
  120. public static String txt2String(File file) {
  121. StringBuilder result = new StringBuilder();
  122. try {
  123. BufferedReader br = new BufferedReader(new FileReader(file));// 构造一个BufferedReader类来读取文件
  124. String s = null;
  125. while ((s = br.readLine()) != null) {// 使用readLine方法,一次读一行
  126. result.append(System.lineSeparator() + s);
  127. }
  128. br.close();
  129. } catch (Exception e) {
  130. e.printStackTrace();
  131. }
  132. return result.toString();
  133. }
  134.  
  135. /**
  136. * 以行为单位读取文件,常用于读面向行的格式化文件
  137. */
  138. public static void readFileByLines(String fileName) {
  139. File file = new File(fileName);
  140. BufferedReader reader = null;
  141. try {
  142. System.out.println("以行为单位读取文件内容,一次读一整行:");
  143. reader = new BufferedReader(new FileReader(file));
  144. String tempString = null;
  145. int line = 1;
  146. // 一次读入一行,直到读入null为文件结束
  147. while ((tempString = reader.readLine()) != null) {
  148. // 显示行号
  149. System.out.println("line " + line + ": " + tempString);
  150.  
  151. line++;
  152. }
  153. reader.close();
  154. } catch (IOException e) {
  155. e.printStackTrace();
  156. } finally {
  157. if (reader != null) {
  158. try {
  159. reader.close();
  160. } catch (IOException e1) {
  161. }
  162. }
  163. }
  164. }
  165.  
  166. /**
  167. * 获取路径下的所有文件/文件夹
  168. *
  169. * @param directoryPath
  170. * 需要遍历的文件夹路径
  171. * @param isAddDirectory
  172. * 是否将子文件夹的路径也添加到list集合中
  173. * @return
  174. */
  175. public static List<String> getAllFile(String directoryPath, boolean isAddDirectory) {
  176. List<String> list = new ArrayList<String>();
  177. File baseFile = new File(directoryPath);
  178. if (baseFile.isFile() || !baseFile.exists()) {
  179. return list;
  180. }
  181. File[] files = baseFile.listFiles();
  182. for (File file : files) {
  183. if (file.isDirectory()) {
  184. if (isAddDirectory) {
  185. list.add(file.getAbsolutePath());
  186. }
  187. list.addAll(getAllFile(file.getAbsolutePath(), isAddDirectory));
  188. } else {
  189.  
  190. list.add(file.getAbsolutePath());
  191. fileName.add(file.getName());
  192. }
  193. }
  194. return list;
  195. }
  196.  
  197. public static String minify(String jsonString) {
  198. boolean in_string = false;
  199. boolean in_multiline_comment = false;
  200. boolean in_singleline_comment = false;
  201. char string_opener = 'x';
  202.  
  203. StringBuilder out = new StringBuilder();
  204. for (int i = 0; i < jsonString.length(); i++) {
  205. char c = jsonString.charAt(i);
  206. String cc = jsonString.substring(i, Math.min(i + 2, jsonString.length()));
  207. if (in_string) {
  208. if (c == string_opener) {
  209. in_string = false;
  210. out.append(c);
  211. } else if (c == '\\') {
  212. out.append(cc);
  213. ++i;
  214. } else
  215. out.append(c);
  216. } else if (in_singleline_comment) {
  217. if (c == '\r' || c == '\n')
  218. in_singleline_comment = false;
  219. } else if (in_multiline_comment) {
  220. if (cc.equals("*/")) {
  221. in_multiline_comment = false;
  222. ++i;
  223. }
  224. } else {
  225. if (cc.equals("/*")) {
  226. in_multiline_comment = true;
  227. ++i;
  228. } else if (cc.equals("//")) {
  229. in_singleline_comment = true;
  230. ++i;
  231. } else if (c == '"' || c == '\'') {
  232. in_string = true;
  233. string_opener = c;
  234. out.append(c);
  235. } else if (!Character.isWhitespace(c))
  236. out.append(c);
  237. }
  238. }
  239. return out.toString();
  240. }
  241. }

  

  

  

Json压缩工具的更多相关文章

  1. seajs中spm压缩工具使用

    seajs是个好东西,用起来很方便,但是她的压缩工具spm确不被网友看好,因为使用起来很麻烦,捯饬了一天多,终于勉强能压缩了,这里就简单记录一下. 按照地址:http://www.zhangxinxu ...

  2. python 开发一款图片压缩工具(四):上传图床

    上一篇使用了 pngquant 图片压缩工具进行压缩,并通过 click 命令行工具构建了 picom 包.这篇的主要功能是实现图片上传. 图片上传功能的实现 通过 pngquant 压缩图片后,得到 ...

  3. Springboot 之 Filter 实现 Gzip 压缩超大 json 对象

    简介 在项目中,存在传递超大 json 数据的场景.直接传输超大 json 数据的话,有以下两个弊端 占用网络带宽,而有些云产品就是按照带宽来计费的,间接浪费了钱 传输数据大导致网络传输耗时较长 为了 ...

  4. 使用TSQL查询和更新 JSON 数据

    JSON是一个非常流行的,用于数据交换的文本数据(textual data)格式,主要用于Web和移动应用程序中.JSON 使用“键/值对”(Key:Value pair)存储数据,能够表示嵌套键值对 ...

  5. 【疯狂造轮子-iOS】JSON转Model系列之二

    [疯狂造轮子-iOS]JSON转Model系列之二 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇<[疯狂造轮子-iOS]JSON转Model系列之一> ...

  6. 【疯狂造轮子-iOS】JSON转Model系列之一

    [疯狂造轮子-iOS]JSON转Model系列之一 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 之前一直看别人的源码,虽然对自己提升比较大,但毕竟不是自己写的,很容易遗 ...

  7. Taurus.MVC 2.2 开源发布:WebAPI 功能增强(请求跨域及Json转换)

    背景: 1:有用户反馈了关于跨域请求的问题. 2:有用户反馈了参数获取的问题. 3:JsonHelper的增强. 在综合上面的条件下,有了2.2版本的更新,也因此写了此文. 开源地址: https:/ ...

  8. .NET Core系列 : 2 、project.json 这葫芦里卖的什么药

    .NET Core系列 : 1..NET Core 环境搭建和命令行CLI入门 介绍了.NET Core环境,本文介绍.NET Core中最重要的一个配置文件project.json的相关内容.我们可 ...

  9. 一个粗心的Bug,JSON格式不规范导致AJAX错误

    一.事件回放  今天工作时碰到了一个奇怪的问题,这个问题很早很早以前也碰到过,不过没想到过这么久了竟然又栽在这里. 当时正在联调一个项目,由于后端没有提供数据接口,于是我直接本地建立了一个 json ...

随机推荐

  1. python解决四舍五入问题

    小数问题是计算机编程中大部分语言都会遇到的问题,尤其是在内容中涉及到评分.金额计算等等,本人一般在解决需求中固定小数位的数字计算时,都会先将其放大整10的倍数至整数,然后计算.存储,只有在显示的时候再 ...

  2. 【剑指Offer】只出现一次的字符

    题目描述 请实现一个函数用来找出字符流中第一个只出现一次的字符.例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g".当从该字符流中读出 ...

  3. cplusplus标准库

    http://www.cplusplus.com/reference/   Standard C++ Library reference C Library The elements of the C ...

  4. 2018-2019-20175334实验二《Java面向对象程序设计》实验报告

    2018-2019-20175334实验二<Java面向对象程序设计>实验报告 一.实验内容及步骤 实验二面向对象程序设计-1 参考 http://www.cnblogs.com/roce ...

  5. Ubuntu 16.04安装Docker-CE

    系统环境 * Ubuntu: 16.04 * Docker: 17.09.0-ce 安装步骤 1.安装Docker-CE,具体参考:https://docs.docker.com/engine/ins ...

  6. mvc中让路由忽略带后缀的路径文件

    public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/ ...

  7. ionic2 调用 cordova非本地化native 插件方法

    1,在项目中添加你的插件     cordova plugin add puginId|puginName|puginUrl|puginPath 2,查看插件clobbers标记 打开项目目录plug ...

  8. Git世界历险记

    Git-版本管理器 Git  ||属于分散型版本管理系统,是为版本管理而而设计的软件.(Linux的创始人Linus Torvalds在2005年开发了Git的原型程序,在此之前人们大多选用Subve ...

  9. Vue 封装可向左向右查看图片列表的组件

    <template> <div class="content-container"> <div class="content-contain ...

  10. k8s 命令补全

    安装:apt-get install bash-completion source <(kubectl completion bash) echo "source <(kubec ...