关于java中常见数据格式的问题(底层的数据结构方面的知识先不研究了…,这里只是记录下偶然发现的问题)

一、定义

数据格式:是描述数据保存在文件或记录中的规则。可以是字符形式的文本格式,或二进制数据形式的压缩格式。字符形式的文本格式占用的存贮空间多但透明度高,二进制数形式的压缩格式占用的存贮空间少但缺少透明度。

二、问题引出

今天想处理excel数据,用Java封装的Jar包去解析,入库时,发现引用的工具对Excel读取时,返回的是 List,并不能真正返回我希望的List,

 [
 [1, 阿拉巴马州, Alabama, AL, 蒙哥马利, Montgomery, null],
 [2, 阿拉斯加州, Alaska, AK, 朱诺, Juneau, null],
 [3, 阿利桑那州, Arizona, AZ, 菲尼克斯, Phoenix, null],
 [4, 阿肯色州, Arkansas, AR, 小石城, Little rock, null],
 [5, 加利福尼亚州, California, CA, 萨克拉门托, Sacramento, null], 
 [6, 科罗拉多州, Colorado, CO, 丹佛, Denver, null], 
 [7, 康涅狄格州, Connecticut, CT, 哈特福德, Hartford, null],
 [8, 特拉华州, Delaware, DE, 多佛, Dover, null], 
 [9, 佛罗里达州, Florida, FL, 塔拉哈西, Tallahassee, null]
  ]

这个数据其实是List的,在处理的时候,先遍历第一层,在把第二层Object强转成List,然后对对象进行Set赋值操作,即可完成解析,返回所希望返回的List。

处理后返回的数据格式为以下:

[
State(id=1, chName=阿拉巴马州, enName=Alabama, code=AL, captial=蒙哥马利, capitalen=Montgomery, remark=null), 
State(id=2, chName=阿拉斯加州, enName=Alaska, code=AK, captial=朱诺, capitalen=Juneau, remark=null), 
State(id=3, chName=阿利桑那州, enName=Arizona, code=AZ, captial=菲尼克斯, capitalen=Phoenix, remark=null), 
State(id=4, chName=阿肯色州, enName=Arkansas, code=AR, captial=小石城, capitalen=Little rock, remark=null), 
State(id=5, chName=加利福尼亚州, enName=California, code=CA, captial=萨克拉门托, capitalen=Sacramento, remark=null), 
State(id=6, chName=科罗拉多州, enName=Colorado, code=CO, captial=丹佛, capitalen=Denver, remark=null), 
State(id=7, chName=康涅狄格州, enName=Connecticut, code=CT, captial=哈特福德, capitalen=Hartford, remark=null), 
State(id=8, chName=特拉华州, enName=Delaware, code=DE, captial=多佛, capitalen=Dover, remark=null), 
State(id=9, chName=佛罗里达州, enName=Florida, code=FL, captial=塔拉哈西, capitalen=Tallahassee, remark=null)
]

三、Java中数据传输几种格式

3.1 JSON

基本格式

{
   
"BigIntSupported": 995815895020119788889,
"date": "20180322",
"message": "Success !",
"status": 200,
"city": "北京",
"count": 632,
"data": {
   
"shidu": "34%",
"pm25": 73,
"pm10": 91,
"quality": "良",
"wendu": "5",
"ganmao": "极少数敏感人群应减少户外活动",
"yesterday": {
   
"date": "21日星期三",
"sunrise": "06:19",
"high": "高温 11.0℃",
"low": "低温 1.0℃",
"sunset": "18:26",
"aqi": 85,
"fx": "南风",
"fl": "<3级",
"type": "多云",
"notice": "阴晴之间,谨防紫外线侵扰"
},
"forecast": [
{
   
"date": "22日星期四",
"sunrise": "06:17",
"high": "高温 17.0℃",
"low": "低温 1.0℃",
"sunset": "18:27",
"aqi": 98,
"fx": "西南风",
"fl": "<3级",
"type": "晴",
"notice": "愿你拥有比阳光明媚的心情"
},
{
   
"date": "23日星期五",
"sunrise": "06:16",
"high": "高温 18.0℃",
"low": "低温 5.0℃",
"sunset": "18:28",
"aqi": 118,
"fx": "无持续风向",
"fl": "<3级",
"type": "多云",
"notice": "阴晴之间,谨防紫外线侵扰"
},
{
   
"date": "24日星期六",
"sunrise": "06:14",
"high": "高温 21.0℃",
"low": "低温 7.0℃",
"sunset": "18:29",
"aqi": 52,
"fx": "西南风",
"fl": "<3级",
"type": "晴",
"notice": "愿你拥有比阳光明媚的心情"
},
{
   
"date": "25日星期日",
"sunrise": "06:13",
"high": "高温 22.0℃",
"low": "低温 7.0℃",
"sunset": "18:30",
"aqi": 71,
"fx": "西南风",
"fl": "<3级",
"type": "晴",
"notice": "愿你拥有比阳光明媚的心情"
},
{
   
"date": "26日星期一",
"sunrise": "06:11",
"high": "高温 21.0℃",
"low": "低温 8.0℃",
"sunset": "18:31",
"aqi": 97,
"fx": "西南风",
"fl": "<3级",
"type": "多云",
"notice": "阴晴之间,谨防紫外线侵扰"
}
]
}
}

优点
a.数据格式比较简洁,也便于阅读和解析,
b.具有自我描述性,易于阅读编写,也易于机器解析与生成
c.使用 Javascript语法来描述数据对象,但是 JSON 仍然独立于语言和平台。JSON 解析器和 JSON 库支持许多不同的编程语言。 目前非常多的动态(PHP,JSP,.NET)编程语言都支持JSON
d.非常适用于服务器与JavaScript交互

3.2 XML(eXtensible Markup Language)

基本格式:

<note>
	<to>George</to>
	<from>John</from>
	<heading>Reminder</heading>
	<body>Don't forget the meeting!</body>
</note>

是基于标签库的数据,优点:
a.读取XML文档
b.使用XML DOM循环遍历文档
c.读取值并存储与变量

3.3 YAML(Yet Another Markup Language)

基本格式

#YAML格式
environments:
    dev:
        url: http://dev.bar.com
        name: Developer Setup
    prod:
        url: http://foo.bar.com
        name: My Cool App
my:
    servers:
        - dev.bar.com
        - foo.bar.com

springtoot架构中常见的
优点:
a.由于实现简单,解析成本低,特别适合在脚本语言中使用
b.YAML比较适合做序列化。因为它是宿主语言数据类型直转的。
c.YAML做配置文件也不错。比如Ruby on Rails的配置就选用的YAML。

四、java 中常见的数据格式问题

4.1 List
[
State(id=1, chName=E, enName=D, code=C, captial=B, capitalen=A, remark=F), 
State(id=1, chName=E, enName=D, code=C, captial=B, capitalen=A, remark=F), 
State(id=1, chName=E, enName=D, code=C, captial=B, capitalen=A, remark=F), 
State(id=1, chName=E, enName=D, code=C, captial=B, capitalen=A, remark=F),
State(id=1, chName=E, enName=D, code=C, captial=B, capitalen=A, remark=F)
]

转成Json后

[
{"capitalen":"A","code":"C","enName":"D","remark":"F","id":1,"captial":"B","chName":"E"},
{"capitalen":"A","code":"C","enName":"D","remark":"F","id":1,"captial":"B","chName":"E"},
{"capitalen":"A","code":"C","enName":"D","remark":"F","id":1,"captial":"B","chName":"E"},
{"capitalen":"A","code":"C","enName":"D","remark":"F","id":1,"captial":"B","chName":"E"},
{"capitalen":"A","code":"C","enName":"D","remark":"F","id":1,"captial":"B","chName":"E"}
]
4.2 Map
{a=State(id=1, chName=E, enName=D, code=C, captial=B, capitalen=A, remark=F), 
b=State(id=1, chName=E, enName=D, code=C, captial=B, capitalen=A, remark=F), 
c=State(id=1, chName=E, enName=D, code=C, captial=B, capitalen=A, remark=F)}

转成Json后

{
"a": {
"capitalen": "A",
"code": "C",
"enName": "D",
"remark": "F",
"id": 1,
"captial": "B",
"chName": "E"
},
"b": {
"capitalen": "A",
"code": "C",
"enName": "D",
"remark": "F",
"id": 1,
"captial": "B",
"chName": "E"
},
"c": {
"capitalen": "A",
"code": "C",
"enName": "D",
"remark": "F",
"id": 1,
"captial": "B",
"chName": "E"
}
}
4.3 Pojo
State(id=1, chName=E, enName=D, code=C, captial=B, capitalen=A, remark=F)

对应的Json

{
"capitalen": "A",
"code": "C",
"enName": "D",
"remark": "F",
"id": 1,
"captial": "B",
"chName": "E"
}
4.4 Array
这里其实是数组的底层的是引用数据类型,toString方法只是显示其在内存中的位置

转成Json后

["s","a","f","g","h"]

备注,这里的Pojo转String,默认是其toString方法

    public String toString() {
        return "McnRecordVo(" +
                "programId=" + programId +
                ", contentId=" + contentId +
                ')';
    }

是可以重写其ToString方法,这样的返回就可以和Map格式一模一样了

  @Override
    public String toString() {
        return "McnRecordVo{" +
                "programId=" + programId +
                ", contentId=" + contentId +
                '}';
    }