JSON数据的优化

JSON数据的可以优化吗?肯定可以,之前都是使用XML,但之后用XML越来越少了,就是因为XML包含的冗余信息太多了。那么JSON数据有没有冗余呢?

肯定有,名称就是重复的!!

例如如下的JSON数据:

{"total":28,"rows":[
    {"productid":"FI-SW-01","productname":"Koi","unitcost":10.00,"status":"P","listprice":36.50,"attr1":"Large","itemid":"EST-1"},
    {"productid":"K9-DL-01","productname":"Dalmation","unitcost":12.00,"status":"P","listprice":18.50,"attr1":"Spotted Adult Female","itemid":"EST-10"},
    {"productid":"RP-SN-01","productname":"Rattlesnake","unitcost":12.00,"status":"P","listprice":38.50,"attr1":"Venomless","itemid":"EST-11"},
    {"productid":"RP-SN-01","productname":"Rattlesnake","unitcost":12.00,"status":"P","listprice":26.50,"attr1":"Rattleless","itemid":"EST-12"},
    {"productid":"RP-LI-02","productname":"Iguana","unitcost":12.00,"status":"P","listprice":35.50,"attr1":"Green Adult","itemid":"EST-13"},
    {"productid":"FL-DSH-01","productname":"Manx","unitcost":12.00,"status":"P","listprice":158.50,"attr1":"Tailless","itemid":"EST-14"},
    {"productid":"FL-DSH-01","productname":"Manx","unitcost":12.00,"status":"P","listprice":83.50,"attr1":"With tail","itemid":"EST-15"},
    {"productid":"FL-DLH-02","productname":"Persian","unitcost":12.00,"status":"P","listprice":23.50,"attr1":"Adult Female","itemid":"EST-16"},
    {"productid":"FL-DLH-02","productname":"Persian","unitcost":12.00,"status":"P","listprice":89.50,"attr1":"Adult Male","itemid":"EST-17"},
    {"productid":"AV-CB-01","productname":"Amazon Parrot","unitcost":92.00,"status":"P","listprice":63.50,"attr1":"Adult Male","itemid":"EST-18"}
]}

如果我们假定第一列是productid,第二列是productname,其他的依次类推,是不是就可以减少近一半的数据呢?

{"total":28,"rows":[
    {"FI-SW-01","Koi",10.00,"P",36.50,"Large","EST-1"},
    {"K9-DL-01","Dalmation",12.00,"P",18.50,"Spotted Adult Female","EST-10"},
    {"RP-SN-01","Rattlesnake",12.00,"P",38.50,"Venomless","EST-11"},
    {"RP-SN-01","Rattlesnake",12.00,"P",26.50,"Rattleless","EST-12"},
    {"RP-LI-02","Iguana",12.00,"P",35.50,"Green Adult","EST-13"},
    {"FL-DSH-01","Manx",12.00,"P",158.50,"Tailless","EST-14"},
    {"FL-DSH-01","Manx",12.00,"P",83.50,"With tail","EST-15"},
    {"FL-DLH-02","Persian",12.00,"P",23.50,"Adult Female","EST-16"},
    {"FL-DLH-02","Persian",12.00,"P",89.50,"Adult Male","EST-17"},
    {"AV-CB-01","Amazon Parrot",92.00,"P",63.50,"Adult Male","EST-18"}
]}

其实这就是csv类似的文件格式,当然这种改动比较大,可以说直接改变了数据结构,影响比较大。

当然也可以采用给个字段重新命名的方式修改,例如:

var attr1={"A":"productid","B":"productname","C":"unitcost","D":"status","E":"listprice","F":"attr1","G":"itemid"}
{"total":28,"rows":[
    {"A":"FI-SW-01","B":"Koi","C":10.00,"D":"P","E":36.50,"F":"Large","G":"EST-1"},
    {"A":"K9-DL-01","B":"Dalmation","C":12.00,"D":"P","E":18.50,"F":"Spotted Adult Female","G":"EST-10"},
    {"A":"RP-SN-01","B":"Rattlesnake","C":12.00,"D":"P","E":38.50,"F":"Venomless","G":"EST-11"},
    {"A":"RP-SN-01","B":"Rattlesnake","C":12.00,"D":"P","E":26.50,"F":"Rattleless","G":"EST-12"},
    {"A":"RP-LI-02","B":"Iguana","C":12.00,"D":"P","E":35.50,"F":"Green Adult","G":"EST-13"},
    {"A":"FL-DSH-01","B":"Manx","C":12.00,"D":"P","E":158.50,"F":"Tailless","G":"EST-14"},
    {"A":"FL-DSH-01","B":"Manx","C":12.00,"D":"P","E":83.50,"F":"With tail","G":"EST-15"},
    {"A":"FL-DLH-02","B":"Persian","C":12.00,"D":"P","E":23.50,"F":"Adult Female","G":"EST-16"},
    {"A":"FL-DLH-02","B":"Persian","C":12.00,"D":"P","E":89.50,"F":"Adult Male","G":"EST-17"},
    {"A":"AV-CB-01","B":"Amazon Parrot","C":92.00,"D":"P","E":63.50,"F":"Adult Male","G":"EST-18"}
]}

 

出处:https://blog.csdn.net/xuexiaodong009/article/details/51810566

posted on 2018-08-03 13:23  jack_Meng  阅读(1940)  评论(0编辑  收藏  举报

导航