JSON数据格式
JSON 是一种数据传输格式 JSON 是要结合 Ajax(异步请求) 使用的, 在后端一般会将一个对象转换成 JSON 格式的数据之后返回给客户端, 可以自己写工具转换, 也可以使用第三方工具 : gjson, fastjson 等Demo: JSON 表示一个数字
2.90Demo: JSON 表示一个字符串
"Hello world"Demo: JSON 表示一个对象
{ "name":"smith". "age":30, "sex":"男"}Demo: JSON对象的属性可以是对象
{ "name":"smith". "age":28, "sex":"男" "school":{ "sname":"南京大学". "address":"南京市鼓楼区汉口路22号" }}Demo: JSON 格式表示数组
保存名字的数组: ["张三","李四","王五"]保存雇员的信息: ["smith",1001,"clerck",7788,2000.00,200.0][ ["smith",1001,"clerck",7788,2000.00,200.0] ["smith",1001,"clerck",7788,2000.00,200.0] ["smith",1001,"clerck",7788,2000.00,200.0]][ {"name":"smith","empno":1001,"job":"clerck","sal":9000.00,"comm":5000.00}, {"name":"smith","empno":1001,"job":"clerck","sal":9000.00,"comm":5000.00}, {"name":"smith","empno":1001,"job":"clerck","sal":9000.00,"comm":5000.00},]Demo: 对象数组
在一个数组保存多个 json 对象 (在一个数组中保存多个对象)[ { "title":"Java 开发", "edition":3, "author":["smith","张三","李四"] }, { "title":"Web 开发", "edition":3, "author":["Allen","王五","赵六"] }]二维数组保存[ ["Java 开发",3,["smith","张三","李四"]], ["Web 开发",3["Allen","王五","赵六"]]]Demo: 将一个对象转换成 json 数据
@WebServlet(urlPatterns= {"/emp/*"})public class EmpServlet extends BaseServlte{ private IEmpService empservice = (IEmpService)ServiceFactory.getInstance(EmpServiceImpl.class); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String pathInfo = req.getPathInfo(); try { if ("/getOne".equals(pathInfo))) { this.getOne(req, resp); } } catch (Exception e) { e.printStackTrace(); } } //将一个对象转换成 json 数据输出到客户端 public void getOne(HttpServletRequest req, HttpServletResponse resp) throws Exception { //获取要查询的雇员编号 Integer id = Integer.parseInt(req.getParameter("id")); Emp emp = empservice.findEmpById(id); //将查询到的对象转换成json 数据格式 String jsonEmp = JSON.toJSONString(emp); System.out.println(jsonEmp); PrintWriter out = null; out=resp.getWriter(); //将转换后的 json 数据输出到客户端 out.print(jsonEmp); out.close(); }}
Demo: 将一个 List 集合转换为 json 数据
@WebServlet(urlPatterns= {"/emp/*"})public class EmpServlet extends BaseServlte{ private IEmpService empservice = (IEmpService)ServiceFactory.getInstance(EmpServiceImpl.class); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String pathInfo = req.getPathInfo(); try { if ("/getOne".equals(pathInfo))) { this.getOne(req, resp); }else if ("/jsonList".equals(pathInfo)) { this.jsonList(req, resp); } } catch (Exception e) { e.printStackTrace(); } } //模糊分页查询: 将一个 List 集合转换为 json 数据 public void jsonList(HttpServletRequest req, HttpServletResponse resp) throws Exception { String kw = req.getParameter("kw"); Integer cp = Integer.parseInt(req.getParameter("cp")); Integer ls = Integer.parseInt(req.getParameter("ls")); //将 List 集合转换为 json 数据格式 String jsonListEmp = JSON.toJSONString(this.empservice.findAllSplit(kw, cp, ls).get("emplist")); System.out.println(jsonListEmp); }}
Demo: 将Map 数据转换为 json 数据
转换 Mao 集合则是键值对的形式@WebServlet(urlPatterns= {"/emp/*"})public class EmpServlet extends BaseServlte{ private IEmpService empservice = (IEmpService)ServiceFactory.getInstance(EmpServiceImpl.class); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String pathInfo = req.getPathInfo(); try { if ("/getOne".equals(pathInfo))) { this.getOne(req, resp); }else if ("/jsonList".equals(pathInfo)) { this.jsonList(req, resp); } else if ("/jsonMap".equals(pathInfo)) { this.jsonMap(req, resp); } } catch (Exception e) { e.printStackTrace(); } } //模糊分页查询: 将一个 Map 集合转换为 json 数据 public void jsonMap(HttpServletRequest req, HttpServletResponse resp) throws Exception { String kw = req.getParameter("kw"); Integer cp = Integer.parseInt(req.getParameter("cp")); Integer ls = Integer.parseInt(req.getParameter("ls")); //将 Map 集合转换为 json 数据格式 String jsonMapEmp = JSON.toJSONString(this.empservice.findAllSplit(kw, cp, ls)); System.out.println(jsonMapEmp); }}