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

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

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

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

//bat文件的代码如下:
set assetsAPath="..\javaOutput\assets\uijson" 原json文件路径
set assetsBPath="..\javaOutput\assets\uijson\\"  压缩后的json文件路径
set targetJar="JSONMinify.jar"
cd ./lang
java -jar lib/JSONMinify.jar
cd ..
cd ./tools
java -jar %targetJar% resVersion=%resVersion% assetsAPath=%assetsAPath% assetsBPath=%assetsBPath% pause

  //java代码如下

package com.pack;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import com.zhaohe.download.ResourceDownloader; public class JSONMinify { static ArrayList<String> fileName; public static void main(String[] args) throws Exception {
initArgs(args);
String fileData1 = assetsAPath;
String fileDatas2 = assetsBPath; fileName = new ArrayList<String>();
File folder = new File(fileDatas2);
List<String> filesPath = getAllFile(fileData1, false); System.err.println(getAllFile(fileData1, false).size()); System.err.println(fileData1); for (int i = 0; i < getAllFile(fileData1, false).size(); i++) {
System.err.println(filesPath.get(i)); File file = new File(filesPath.get(i));// 需要读取的文件
File file1 = new File(fileDatas2, fileName.get(i));// 需要写进去的文件
String fileContext = txt2String(file);// 文件内容
file.delete();
if (!file1.exists()) {
file.createNewFile();
}
             
         PrintWriter out1 = new PrintWriter(new BuffewedWriter(new OutputStreamWriter(new FileOutputStream(fileDatas2+fileName.get(i)),"utf-8")));
         out1.write(minify(fileContext)); out.write(minify(fileContext));
out.close();
   }
} private static final String manifestFileName = "assetsManifest.txt";
private static final String remoteVersionFile = "assetsVersion.txt";
private static final String lcoalVersionFile = "localAssetsVersion.txt"; private static String version = "1.0.2";
private static String assetsAPath = "..\\android\\assets\\uiname\\";
private static String assetsBPath = "..\\android\\assets\\uijson\\";
private static String rootFolder = ".\\packages";
private static String cdnUrl = "http://www.mathmt.com";
private static String serverID = "12";
private static String resVersion = String.valueOf(ResourceDownloader.RES_TYPE_CURRENT); public static void initArgs(String args[]) {
if (args != null) {
for (String arg : args) {
String[] keyValue = arg.split("=");
final String key = keyValue[0];
final String value = keyValue[1];
if ("targetVersion".equals(key)) {
version = value;
} else if ("assetsAPath".equals(key)) {
assetsAPath = value;
} else if ("assetsBPath".equals(key)) {
assetsBPath = value;
} else if ("cdnUrl".equals(key)) {
cdnUrl = value;
} else if ("rootFolder".equals(key)) {
rootFolder = value;
} else if ("serverID".equals(key)) {
serverID = value;
} else if ("resVersion".equals(key)) {
resVersion = value;
} else {
System.out.println("Unknown key:" + key);
}
}
}
} public static boolean deleteDir(String path) {
File file = new File(path);
if (!file.exists()) {// 判断是否待删除目录是否存在
return false;
} String[] content = file.list();// 取得当前目录下所有文件和文件夹
for (String name : content) {
File temp = new File(path, name);
if (temp.isDirectory()) {// 判断是否是目录
deleteDir(temp.getAbsolutePath());// 递归调用,删除目录里的内容
temp.delete();// 删除空目录
} else {
if (!temp.delete()) {// 直接删除文件
temp.delete();
System.err.println("Failed to delete " + name);
}
}
}
return true;
} /*
* 读取txt文件的内容
*
* @param file 想要读取的文件对象
*
* @return 返回文件内容
*/
public static String txt2String(File file) {
StringBuilder result = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));// 构造一个BufferedReader类来读取文件
String s = null;
while ((s = br.readLine()) != null) {// 使用readLine方法,一次读一行
result.append(System.lineSeparator() + s);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
return result.toString();
} /**
* 以行为单位读取文件,常用于读面向行的格式化文件
*/
public static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
System.out.println("line " + line + ": " + tempString); line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
} /**
* 获取路径下的所有文件/文件夹
*
* @param directoryPath
* 需要遍历的文件夹路径
* @param isAddDirectory
* 是否将子文件夹的路径也添加到list集合中
* @return
*/
public static List<String> getAllFile(String directoryPath, boolean isAddDirectory) {
List<String> list = new ArrayList<String>();
File baseFile = new File(directoryPath);
if (baseFile.isFile() || !baseFile.exists()) {
return list;
}
File[] files = baseFile.listFiles();
for (File file : files) {
if (file.isDirectory()) {
if (isAddDirectory) {
list.add(file.getAbsolutePath());
}
list.addAll(getAllFile(file.getAbsolutePath(), isAddDirectory));
} else { list.add(file.getAbsolutePath());
fileName.add(file.getName());
}
}
return list;
} public static String minify(String jsonString) {
boolean in_string = false;
boolean in_multiline_comment = false;
boolean in_singleline_comment = false;
char string_opener = 'x'; StringBuilder out = new StringBuilder();
for (int i = 0; i < jsonString.length(); i++) {
char c = jsonString.charAt(i);
String cc = jsonString.substring(i, Math.min(i + 2, jsonString.length()));
if (in_string) {
if (c == string_opener) {
in_string = false;
out.append(c);
} else if (c == '\\') {
out.append(cc);
++i;
} else
out.append(c);
} else if (in_singleline_comment) {
if (c == '\r' || c == '\n')
in_singleline_comment = false;
} else if (in_multiline_comment) {
if (cc.equals("*/")) {
in_multiline_comment = false;
++i;
}
} else {
if (cc.equals("/*")) {
in_multiline_comment = true;
++i;
} else if (cc.equals("//")) {
in_singleline_comment = true;
++i;
} else if (c == '"' || c == '\'') {
in_string = true;
string_opener = c;
out.append(c);
} else if (!Character.isWhitespace(c))
out.append(c);
}
}
return out.toString();
}
}

  

  

  

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量化分析,计算KDJ

    Python: v3.6 Pandas: v0.23.4 使用以下方法计算与国内财经软件显示一致 low_list = df['最低价'].rolling(9, min_periods=9).min( ...

  2. iview 3.x InputNumber数字框bug

    iview 3.X 版本中InputNumber 数字框组件存在bug,把最小值设置为0.2时,数组框禁止点击,其他数字都是正常.

  3. MYSQL-联合索引

    深入理解 index merge 是使用索引进行优化的重要基础之一.理解了 index merge 技术,我们才知道应该如何在表上建立索引. 1. 为什么会有index merge 我们的 where ...

  4. IoC容器的接口设计

    1.从接口BeanFactory---HierarchicalBeanFactory---ConfigurableBeanFactory,是一条主要的BeanFactory设计路径. 2.第二条接口设 ...

  5. LeetCode【104. 二叉树的最大深度】

    最开始的想法就是递归,但是,自己想的太麻烦,每个节点与null相比较,如果都不为null,count就加一,然后输出count, 其实,这中间有很多错误,然后,就想着想着就绕不出来了.然后,重新思考了 ...

  6. 转载一份kaggle的特征工程:经纬度、特征构造、转化率

    转载:https://www.toutiao.com/i6642477603657613831/ 1 如果训练/测试都来自同一时间线,那么就可以非常巧妙地使用特性.虽然这只是一个kaggle的案例,但 ...

  7. C# 创建Datatable 并插入数据

    DataTable dt_temp = new DataTable(); dt_temp.Columns.Add("id"); dt_temp.Columns.Add(" ...

  8. 使用Microsoft自带的小工具将可执行文件(.exe)注册为系统服务

    首先,我们从Microsoft下载Windows Resource Kits,Download 下载完成后,运行rktools.exe进行安装. 安装完成后,我们打开安装目录,将其中的"in ...

  9. Cesium学习网址

    不错的案例介绍: 根据地形瓦片直接绘制高程.坡度及等高线 同一场景下显示两个不同的瓦片图层 https://cloud.tencent.com/developer/article/1113355 绘制 ...

  10. php的运行流程

    1.Zend引擎:Zend整体用纯C实现,是PHP的内核部分,他将PHP代码翻译(词法.语法解析等一系列编译过程)为可执行opcode的处理并实现相应的处理方法.实现了基本的数据结构(如:hashta ...