曾经一段时间里xml成为了互联网业界内的数据传输格式标准,但是有人对xml提出了质疑,认为xml数据格式比较繁杂,冗长等弊端,于是提出了很多解决方案
在2001年的时候JSON数据格式就出现了,并且在2006年的时候在互联网业界内开始普及使用,JSON是一种数据传输格式,而不是一门语言,基本上所有的编程语言都支持JSON数据格式
表示一个数字
2.90
表示一个字符串
"Hello World"
{
"name":"smith",
"age":30,
"sex":男
}
以上就是用JSON表示一个对象,对象的属性也使用双引号括起来,这是一种标准写法
JSON中的属性还可以是对象
{
"name":"smith",
"age":18
"sex":男
"school":{
"sname":"南京大学",
"address":"南京市鼓楼区汉口路22号"
}
}
保存名字的数字:["尼古拉斯","斯巴达","雅典娜"]
保存雇员的信息:["尼古拉斯",1001,"总裁",7788,50000.00,10000.0]
在一个数字中保存多个JSON对象
Demo
[
{
"title":"Java实战经典开发",
"edition":3,
"author":["smith","尼古拉斯","斯巴达"]
},
{
"title":"Oracle实战经典开发",
"edition":3,
"author":["smith","尼古拉斯","斯巴达"]
},
{
"title":"Vue实战经典开发",
"edition":5,
"author":["smith","尼古拉斯","斯巴达"]
}
]
还可以将上面的Demo转换成二维数组保存
[
["Java实战经典开发",3,["smith","尼古拉斯","斯巴达"]]
["Oracle实战经典开发",3,["smith","尼古拉斯","斯巴达"]]
["Vue实战经典开发",5,["smith","尼古拉斯","斯巴达"]]
]
[
["smith",1001,"clerck",7788,2000.00]
["smith",1001,"clerck",7788,2000.00]
["smith",1001,"clerck",7788,2000.00]
]
https://suggest.taobao.com/sug?code=utf-8&q=衣服&callback=cb
cb({"result":
[
["衣服女2019新品","58601.472377312675"],
["衣服女春装","1014295.8219525206"],
["衣服男","2753566.216334307"],
["衣服女","3272979.671592127"],
["衣服女春装套装","2919470.42070989"],
["衣服男潮流","8861277.460102722"],
["衣服男春季","3844818.252672986"],
["衣服女春装 百搭","1721343.09479825"],
["衣服女春装 韩版学生","843555.8009733238"],
["衣服女韩版","10308716.408744132"]
]
})
JSON是要结合Ajax(异步请求)使用的
在Java中有很多第三方工具能将一个Java的对象转换成JSON数据格式,比如阿里巴巴开发的fastjson
public void getEmpById(HttpServletRequest req, HttpServletResponse resp) {
//取得业务层实现类对象
IEmpService service = new EmpServiceImpl();
//取得要查询的雇员的编号
Integer empno = Integer.parseInt(req.getParameter("id"));
//将查询到的数据对象转换成JSON数据
String json = JSON.toJSONString(service.findEmpById(empno));
//将JSON数据直接输出给客户端(而不是将数据保存到request内置对象,跳转到JSP,使用EL表达式输出)
PrintWriter out = null;
try {
out = resp.getWriter();
out.print(json);
} catch (Exception e) {
e.printStackTrace();
}
out.close();
}
{
"deptno":10,
"empno":7839,
"ename":"KING",
"hiredate":373910400000,
"job":"PRESIDENT",
"sal":5000.0
}
@Override
public Map<String, Object> findAllSplit(String kw, Integer cp, Integer ls) {
Map<String, Object> map = new HashMap<String, Object>();
try {
/*将list集合中的雇员按照职位进行分组,以键值对的形式保存到map集合中,
key是职位名称,value是该职位下所有雇员的List集合 */
List<Emp> list = this.empDAO.selectSplitAll(kw, cp, ls);
//调用分组的方法
//this.groupByJob(list, map);
map.put("list", list);
} catch (Exception e) {
e.printStackTrace();
}finally {
C3P0Util.close(conn);
}
return map;
}
public void getEmpList(HttpServletRequest req, HttpServletResponse resp) {
IEmpService service = new EmpServiceImpl();
PrintWriter out = null;
try {
out=resp.getWriter();
//将list集合转换成JSON输出
out.println(JSON.toJSONString(service.findAllSplit("", 1, 100).get("list")));
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
[
{"deptno":20,"empno":7566,"ename":"JONES","hiredate":354988800000,"job":"MANAGER","mgr":7839,"sal":2975.0},
{"comm":1400.0,"deptno":30,"empno":7654,"ename":"MARTIN","hiredate":370454400000,"job":"SALESMAN","mgr":7698,"sal":1250.0},
{"deptno":30,"empno":7698,"ename":"BLAKE","hiredate":352224000000,"job":"MANAGER","mgr":7839,"sal":2850.0},
{"deptno":10,"empno":7782,"ename":"CLARK","hiredate":363456000000,"job":"MANAGER","mgr":7839,"sal":2450.0},
{"deptno":10,"empno":7839,"ename":"KING","hiredate":373910400000,"job":"PRESIDENT","sal":5000.0},
{"comm":0.0,"deptno":30,"empno":7844,"ename":"TURNER","hiredate":368726400000,"job":"SALESMAN","mgr":7698,"sal":1500.0},
{"deptno":20,"empno":7876,"ename":"ADAMS","hiredate":411148800000,"job":"CLERK","mgr":7788,"sal":1100.0},
{"deptno":30,"empno":7900,"ename":"JAMES","hiredate":376156800000,"job":"CLERK","mgr":7698,"sal":950.0},
{"deptno":10,"empno":7934,"ename":"MILLER","hiredate":379612800000,"job":"CLERK","mgr":7782,"sal":1300.0},
{"comm":90.0,"deptno":10,"empno":7788,"ename":"xs","hiredate":1552579200000,"job":"??","mgr":7780,"sal":8500.0},
{"comm":10000.0,"deptno":10,"empno":1002,"ename":"xs","hiredate":1552665600000,"job":"??","mgr":7369,"sal":50000.0},
{"comm":10000.0,"deptno":10,"empno":1002,"ename":"xs","hiredate":1552665600000,"job":"??","mgr":7369,"sal":50000.0},
{"comm":10000.0,"deptno":10,"empno":7788,"ename":"xs","hiredate":1552665600000,"job":"??","mgr":7369,"sal":50000.0}
]
@Override
public Map<String, Object> findAllSplit(String kw, Integer cp, Integer ls) {
Map<String, Object> map = new HashMap<String, Object>();
try {
/*将list集合中的雇员按照职位进行分组,以键值对的形式保存到map集合中,
key是职位名称,value是该职位下所有雇员的List集合 */
List<Emp> list = this.empDAO.selectSplitAll(kw, cp, ls);
//调用分组的方法
this.groupByJob(list, map);
} catch (Exception e) {
e.printStackTrace();
}finally {
C3P0Util.close(conn);
}
return map;
}
public void getEmpList(HttpServletRequest req, HttpServletResponse resp) {
IEmpService service = new EmpServiceImpl();
PrintWriter out = null;
try {
out=resp.getWriter();
//将list集合转换成JSON输出
out.println(JSON.toJSONString(service.findAllSplit("", 1, 100)));
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
{
"??":
[{"comm":90.0,"deptno":10,"empno":7788,"ename":"xs","hiredate":1552579200000,"job":"??","mgr":7780,"sal":8500.0}],
"SALESMAN":
[{"comm":1400.0,"deptno":30,"empno":7654,"ename":"MARTIN","hiredate":370454400000,"job":"SALESMAN","mgr":7698,"sal":1250.0},
{"comm":0.0,"deptno":30,"empno":7844,"ename":"TURNER","hiredate":368726400000,"job":"SALESMAN","mgr":7698,"sal":1500.0}],
"CLERK":
[{"deptno":20,"empno":7876,"ename":"ADAMS","hiredate":411148800000,"job":"CLERK","mgr":7788,"sal":1100.0},
{"deptno":30,"empno":7900,"ename":"JAMES","hiredate":376156800000,"job":"CLERK","mgr":7698,"sal":950.0},
{"deptno":10,"empno":7934,"ename":"MILLER","hiredate":379612800000,"job":"CLERK","mgr":7782,"sal":1300.0}],
"PRESIDENT":
[{"deptno":10,"empno":7839,"ename":"KING","hiredate":373910400000,"job":"PRESIDENT","sal":5000.0}],"MANAGER":
[{"deptno":20,"empno":7566,"ename":"JONES","hiredate":354988800000,"job":"MANAGER","mgr":7839,"sal":2975.0},
{"deptno":30,"empno":7698,"ename":"BLAKE","hiredate":352224000000,"job":"MANAGER","mgr":7839,"sal":2850.0},
{"deptno":10,"empno":7782,"ename":"CLARK","hiredate":363456000000,"job":"MANAGER","mgr":7839,"sal":2450.0}],
"??":
[{"comm":10000.0,"deptno":10,"empno":1002,"ename":"xs","hiredate":1552665600000,"job":"??","mgr":7369,"sal":50000.0},
{"comm":10000.0,"deptno":10,"empno":1002,"ename":"xs","hiredate":1552665600000,"job":"??","mgr":7369,"sal":50000.0},
{"comm":10000.0,"deptno":10,"empno":7788,"ename":"xs","hiredate":1552665600000,"job":"??","mgr":7369,"sal":50000.0}]}