JSON數據格式
JSON 是一種數據傳輸格式
JSON 是要結合 Ajax(異步請求) 使用的,
在后端一般會將一個對象轉換成 JSON 格式的數據之后返回給客戶端,
可以自己寫工具轉換, 也可以使用第三方工具 : gjson, fastjson 等
Demo: JSON 表示一個數字
2.90
Demo: 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);
}
}
