使JSON响应更小的...只是一个想法 [英] Making JSON responses even smaller... just an idea

查看:93
本文介绍了使JSON响应更小的...只是一个想法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是一个想法真的...并想知道如果用gzip压缩的JSON已经涵盖了这一点。

Just a thought really... and wondering if Gzipped JSON already covers this.

但是,假设你有游戏中的对象响应列表:

But say you have a list of game objects in a response:

game = {
    name: 'Randomer Quest!',
    description: 'Randomer's Quest is a brilliant game!',
    activated: true,
    points: 10,
    thumb: 'randomer-quest.jpg'
};

在json_en code这一点,就变成 151字节

When you json_encode this, it becomes 151 bytes:

{"games": [{"name":"Randomer Quest!","description":"Randomer's Quest is a brilliant game!","activated":true,"points":10,"thumb":"randomer-quest.jpg"}]}

好......但如果​​你有大约100游戏列表?这是关于 13913字节 ...但是我们真的需要保留,宣布这些属性? 我知道你可以在c它和环路只是去$ C $通过它(魔术),但是,如果我们是一个小更智能的它,并在一个单独的对象申报的属性,然后有数据的数组?我们不得不pre灌注的属性,不存在一般,但我仍然认为它是值得的。

Ok... but what if you have a list of about 100 games? That's about 13,913 bytes... but do we really need to keep declaring those properties? I know you can just decode it and loop through it (the magic) but what if we're a little more intelligent about it and declare the properties in a seperate object and then have an array of data? We'd have to prefill properties that aren't there usually but I still think its worth it.

事情是这样的:

{
"games": {
    p: ["name", "description", "activated", "points", "thumb"],
    d: [
        ["Randomer Quest!", "Randomer's Quest is a brilliant game!", true, 10, "randomer-quest.jpg"],
        ["Randomer Quest!", "Randomer's Quest is a brilliant game!", true, 10, "randomer-quest.jpg"]
    ]
}

}

P的性质,D是在阵列中的数据。然后我们有:<!code> 9377字节尺寸的67%

P are properties, D is the data in arrays. Afterwards we have: 9,377 bytes 67% of the size!

好吧,我知道你会说什么,但你看到的更像是40-100kb请求。我认为这是一个相当巨大的差异。任何人都使用这样的事情了吗?也许我们有工具已经这样做自动?

Ok I know you're going to say that's nothing but you do see requests that are more like 40-100kb. And I think that's quite a massive difference. Anyone employing something like this already? Perhaps we have tools that already do this automatically?

32bitkid有pretty的不多说了,如果你要做到这一点,你可能也只是修剪下来到CSV格式...这是有道理的......这将是围绕 9253字节 66.5%。

32bitkid has pretty much said that if you were going to do this, you might as well just trim it down to CSV format... which makes sense... that would be around 9,253 bytes 66.5%.

"name", "description", "activated", "points", "thumb"
"Randomer Quest!", "Randomer's Quest is a brilliant game!", true, 10, "randomer-quest.jpg"
"Randomer Quest!", "Randomer's Quest is a brilliant game!", true, 10, "randomer-quest.jpg"

我已经看到了100KB左右的JSON请求,这将变成66.5kb(失去33.5kb)

I've seen JSON requests of about 100kb, which would turn into 66.5kb (losing 33.5kb)

你怎么看?

大教堂

推荐答案

我同意这是更加紧凑。

{
    "games": {
        p: ["name", "description", "activated", "points", "thumb"],
        d: [
            ["Randomer Quest!", "Randomer's Quest is a brilliant game!", true, 10, "randomer-quest.jpg"],
            ["Randomer Quest!", "Randomer's Quest is a brilliant game!", true, 10, "randomer-quest.jpg"]
        ]
    }
}

别急,你可以进一步优化它,你真正需要的游戏的对象?这是更小!

But wait, you could optimize it further, do you really need the "games" object? this is even smaller!

{
    p: ["name", "description", "activated", "points", "thumb"],
    d: [
        ["Randomer Quest!", "Randomer's Quest is a brilliant game!", true, 10, "randomer-quest.jpg"],
        ["Randomer Quest!", "Randomer's Quest is a brilliant game!", true, 10, "randomer-quest.jpg"]
    ]
}

真的,什么的P和D和包含对象的角度来看,我知道属性名称将是第一个,我的数据会是第二个?

And really, whats the point of the "p" and "d" and the object that contains, i know that the property names are going to be first, and my data is going to be second?

[
    ["name", "description", "activated", "points", "thumb"],
    ["Randomer Quest!", "Randomer's Quest is a brilliant game!", true, 10, "randomer-quest.jpg"],
    ["Randomer Quest!", "Randomer's Quest is a brilliant game!", true, 10, "randomer-quest.jpg"]
]

和这些数组和对象标记刚开的方式,节省了几个字节!

And those array and object markers are just getting in the way, save a few more bytes!

"name", "description", "activated", "points", "thumb"
"Randomer Quest!", "Randomer's Quest is a brilliant game!", true, 10, "randomer-quest.jpg"
"Randomer Quest!", "Randomer's Quest is a brilliant game!", true, 10, "randomer-quest.jpg"

等等......这种格式已经存在。这是 CSV 。自1960年代中期和它的存在了。其中的原因的一部分为什么XML和JSON发明了摆在首位。 JSON和XML增加灵活性,所存储的对象,并让它们比紧凑的二进制对象更可读。你真的那么担心会在管道中的数据的大小?如果你(如果是这样,其实,你的瓶颈),然后有一堆不同的方式来解决这个问题。

Wait... this format already exists. It is CSV. And its been around since the mid 1960's. And its part of the reason why XML and JSON were invented in the first place. JSON and XML add flexibility to the objects being stored and to make them more human readable than tightly packed binary objects. Are you really that worried about the size of the data going over the pipe? If you are (if that is, in fact, your bottleneck) then there are a bunch of different ways to address that problem.

不过,就个人而言,我认为你应该使用的技术和工具,他们正在为制作,以及他们在做的出色。

But, personally, I think you should use the technology and the tools for what they are made for, and what they excel at doing.

您正在试图用锤子在螺丝拧......你会得到它在墙上,但它不会是pretty的或令人愉快的任何一方参与其中。

You're trying to use a hammer to screw in a screw... You'll get it in the wall, but it wont be pretty or pleasant for either party involved.

查找解决你的问题,而不是周围的其他方式的模式。

Find a pattern that solves your problem, not the other way around.

这篇关于使JSON响应更小的...只是一个想法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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