什么是“压缩JSON"? [英] What is "compressed JSON"?

查看:182
本文介绍了什么是“压缩JSON"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当涉及不同的序列化格式时,我会看到很多对压缩JSON"的引用.到底是什么它只是压缩的JSON还是其他?

I see a lot of references to "compressed JSON" when it comes to different serialization formats. What exactly is it? Is it just gzipped JSON or something else?

推荐答案

压缩的JSON删除了json编码的key:value对,以将键和值存储在单独的并行数组中:

Compressed JSON removes the key:value pair of json's encoding to store keys and values in seperate parallel arrays:

// uncompressed
JSON = {
  data : [
     { field1 : 'data1', field2 : 'data2', field3 : 'data3' },
     { field1 : 'data4', field2 : 'data5', field3 : 'data6' },
     .....
  ]
};

//compressed
JSON = {
    data : [ 'data1','data2','data3','data4','data5','data6' ],
    keys : [ 'field1', 'field2', 'field3' ]
};

我在这里找到了这种使用方法

This method of usage i found here

链接内容( http://www.nwhite.net/?p = 242 )

很少能找到我自己编写的使用纯格式使用AJAX的javascript应用程序的地方.我早就放弃了"X",而将其替换为"J"(JSON).使用Javascript时,返回JSON才有意义.自从使用JSON以来,我所获得的所有优点都是占用空间少,解析容易,结构简单.

rarely find myself in a place where I am writing javascript applications that use AJAX in its pure form. I have long abandoned the ‘X’ and replaced it with ‘J’ (JSON). When working with Javascript, it just makes sense to return JSON. Smaller footprint, easier parsing and an easier structure are all advantages I have gained since using JSON.

在最近的一个项目中,我发现自己对我的结果集太大感到不满意.我返回的数据是表格数据,形式为每一行的对象.我返回的结果集为50,每个结果集包含19个字段.我意识到如果扩展结果集,我可以得到某种形式的压缩.

In a recent project I found myself unhappy with the large size of my result sets. The data I was returning was tabular data, in the form of objects for each row. I was returning a result set of 50, with 19 fields each. What I realized is if I augment my result set I could get a form of compression.

//未压缩

JSON = {
  data : [
     { field1 : 'data1', field2 : 'data2', field3 : 'data3' },
     { field1 : 'data4', field2 : 'data5', field3 : 'data6' },
     .....
  ]
};

//压缩

JSON = {
    data : [ 'data1','data2','data3','data4','data5','data6' ],
    keys : [ 'field1', 'field2', 'field3' ]
};

我将所有值合并到单个数组中,并将所有字段存储在单独的数组中.为每个结果返回一个键值对花费了我8800字节(8.6kb).将字段翻录并放入一个单独的数组中需要我186个字节.总共节省8.4kb.

I merged all my values into a single array and store all my fields in a separate array. Returning a key value pair for each result cost me 8800 byte (8.6kb). Ripping the fields out and putting them in a separate array cost me 186 bytes. Total savings 8.4kb.

现在我有一个压缩得多的JSON文件,但是结构不同,现在更难使用.因此,我在Mootools中实现了一个解决方案,以使解压缩透明化.

Now I have a much more compressed JSON file, but the structure is different and now harder to work with. So I implement a solution in Mootools to make the decompression transparent.

Request.JSON.extend({

    options : {
        inflate : []
    }

});




Request.JSON.implement({

    success : function(text){
        this.response.json = JSON.decode(text, this.options.secure);
        if(this.options.inflate.length){
            this.options.inflate.each(function(rule){
                var ret = ($defined(rule.store)) ? this.response.json[rule.store] : this.response.json[rule.data];
                ret = this.expandData(this.response.json[rule.data], this.response.json[rule.keys]);
            },this);
        }
        this.onSuccess(this.response.json, text);
    },

    expandData : function(data,keys){
        var arr = [];
        var len = data.length; var klen = keys.length;
        var start = 0; var stop = klen;
        while(stop < len){
            arr.push( data.slice(start,stop).associate(keys) );
            start = stop; stop += klen;
        }
        return arr;
    }

});

Request.JSON now has an inflate option. You can inflate multiple segments of your JSON object if you so desire.

Usage:

new Request.JSON({
       url : 'url',
       inflate : [{ 'keys' : 'fields', 'data' : 'data' }]
       onComplete : function(json){}
});

根据需要将尽可能多的膨胀对象传递给选项膨胀数组.它具有一个名为"store"的可选属性.如果设置了该属性,则会将膨胀后的数据集存储在该键中.

Pass as many inflate objects as you like to the option inflate array. It has an optional property called ’store’ If set the inflated data set will be stored in that key instead.

键"和字段"期望字符串与JSON对象根目录中的位置匹配.

The ‘keys’ and ‘fields’ expect strings to match a location in the root of your JSON object.

这篇关于什么是“压缩JSON"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆