移动Web开发,数据压缩,后端压缩传输的json格式数据

来源:互联网 发布:js面向对象继承call 编辑:程序博客网 时间:2024/05/18 03:27

最近做了个移动web应用,java平台做后台,后台查询的数据结果用json格式传输,其中有个页面,后台返回的数据量很大,json字符串达到了68K,这对于移动设备的流量和响应速度来说,绝对是个悲剧。

1,未处理前的数据格式为:

{[{"consDept":"A部门","consDeptCode":"001","provinceScheduleVO":[{"projectTypeCode":"DEngineering","percentSchedule":"100","planStartStatus":"2"},{"projectTypeCode":"Main","percentSchedule":"50","planStartStatus":"2"}, ……]},

{"consDept":"B部门","consDeptCode":"002","provinceScheduleVO":[{"projectTypeCode":"DEngineering","percentSchedule":"100","planStartStatus":"1"},{"projectTypeCode":"Main","percentSchedule":"0","planStartStatus":"1"}, ……]}, …… ]};

字符串大小为68K.


2,很明显,字符串越长,体积越大,因为是数组形式,相同的属性名称会重复很多次,通过减短属性名称,应该能降低不少体积。

比如,把consDept属性名改成a,consDeptCode属性名改成b,把projectTypeCode属性名称改成c, ……  切记不要传输前台不需要的属性。

处理后的数据格式为:

{[{"a":"A部门","b":"001","VO":[{"c":"DEngineering","d":"100","e":"2"},{"c":"Main","d":"50","e":"2"}, ……]},

{"a":"B部门","b":"002","VO":[{"c":"DEngineering","d":"100","e":"1"},{"c":"Main","d":"0","e":"1"}, ……]}, …… ]};

字符串大小变成了25K,效果很明显。

对于这种数组形式,如果数据格式相对比较简单,没有嵌套的一维数组,甚至可以改成键值对的形式, 比如 “a”:["A部门","B部门","C部门"] ,以减少“a”属性名称出现的次数。  


3,对结果再采用压缩算法来压缩,(此处演示用ZIP或GZIP压缩)

ZIP压缩,除了压缩外,还会归档成一个文件

GZIP压缩,则只能压缩。本应用无需归档,所以,此处采用GZIP方式压缩字符串。

运用jdk自带的java.util.zip.GZIPInputStream,java.util.zip.GZIPOutputStream 即可完成压缩。

处理后的数据格式为:

"{?\u0001°T?U?????5\u003e\u0010?(\r\u0002Ql?\u0018\u0013\u0014±\u0015\u000bH??\u0016\"b??? ?\"_A?\u0019v???è\u001e??×??I?2Op?ss???????\u000
2??????[?\u001bY~\u001f?_y?yn?\r\u0027??B7?\nm?is6\u000eU?èüH}ì7;úVá*?\u0013Q??\u0012re??W\u0016?\u000f??\u0015?à\u0013?|8Bm??-J7q?V?]\u0013u\\\u003dA???\u001frB??\u000f????\u0005?\u0016?\u0012|T?N??\u0012?p????\u0004Ué[??^?·\u003c???9T?\u001au\n?eE???_\u001fJ?O}……

字符串大小变成了1.4K,节省了不少空间。


附上GZIP压缩java代码:

import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.util.zip.GZIPInputStream;import java.util.zip.GZIPOutputStream; /** * Gzip 压缩字符串 */public class GZIP {  /**  * 字符串的压缩  * @param str 待压缩的字符串  * @return 返回压缩后的字符串  * @throws IOException  */ public static String compress(String str) throws IOException {     if (null == str || str.length() <= 0) {         return str;     }     // 创建一个新的输出流     ByteArrayOutputStream out = new ByteArrayOutputStream();     // 使用默认缓冲区大小创建新的输出流     GZIPOutputStream gzip = new GZIPOutputStream(out);     // 将字节写入此输出流     gzip.write(str.getBytes("utf-8"));  //因为后台默认字符集有可能是GBK字符集,所以此处需指定一个字符集     gzip.close();     // 使用指定的 charsetName,通过解码字节将缓冲区内容转换为字符串     return out.toString("ISO-8859-1"); }  /**  * 字符串的解压  * @param str 对字符串解压  * @return 返回解压缩后的字符串  * @throws IOException  */public static String unCompress(String str) throws IOException {     if (null == str || str.length() <= 0) {         return str;     }     // 创建一个新的输出流     ByteArrayOutputStream out = new ByteArrayOutputStream();     // 创建一个 ByteArrayInputStream,使用 buf 作为其缓冲区数组     ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes("ISO-8859-1"));     // 使用默认缓冲区大小创建新的输入流     GZIPInputStream gzip = new GZIPInputStream(in);     byte[] buffer = new byte[256];     int n = 0;     // 将未压缩数据读入字节数组     while ((n = gzip.read(buffer)) >= 0){           out.write(buffer, 0, n);     }     // 使用指定的 charsetName,通过解码字节将缓冲区内容转换为字符串      return out.toString("utf-8");      }}


                                             
0 0
原创粉丝点击