Json是一种轻量级的数据交换格式,采用一种“键:值”对的文本格式来存储和表示数据,在系统交换数据过程当中经常被使用,是一种理想的数据交换语言。在使用 Java 作 Web 开发时,不可避免的会遇到 Json 的使用。html
咱们先来看如下数据:ajax
{ "ID": "1001", "name": "张三", "age": "24" }
观察它的数据形式,能够得出如下语法:json
遵照上面3点,即可以造成一个json对象。
接下来咱们再看第二个数据:数组
[ {"ID": 1001, "name": "张三", "age": 24}, {"ID": 1002, "name": "李四", "age": 25}, {"ID": 1003, "name": "王五", "age": 22} ]
观察它的数据形式,能够得出如下语法:浏览器
遵照上面3点,即可造成一个 json 对象数组(及一个数组中,存储了多个 json 对象)
理解了上面两种基本的形式,咱们就能够得出其余的数据形式,例以下面这个:服务器
{ "部门名称":"研发部", "部门成员":[ {"ID": 1001, "name": "张三", "age": 24}, {"ID": 1002, "name": "李四", "age": 25}, {"ID": 1003, "name": "王五", "age": 22}], "部门位置":"xx楼21号" }
这是上面两个基本形式结合出来的一种变形,经过这种变形,使得数据的封装具备很大的灵活性,能让开发者自由的发挥想象力。app
总结:json 能够简单的分为基本形式:json 对象,json 对象数组。两种基本格式组合变形出其余的形式,但其本质仍是 json 对象或者 json 对象数组中的一种。json 对象或对象数组能够转化为 json 字符串,使用于不一样的场合。框架
JSON 协议使用方便,愈来愈流行,JSON 的处理器有不少,这里我介绍一下FastJson,FastJson 是阿里的开源框架,被很多企业使用,是一个极其优秀的 Json 框架,Github 地址: FastJson。url
parse(String text);
: 把JSON文本parse为JSONObject或者JSONArrayparseObject(String text);
: 把JSON文本parse成JSONObjectparseArray(String text);
: 把JSON文本parse成JSONArraytoJSONString(Object object);
: 将JavaBean序列化为JSON文本服务器接收请求数据,发送 JSON 回浏览器,并根据不一样的请求方式,分别解决中文乱码问题:spa
1 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 2 request.setCharacterEncoding("utf-8"); 3 response.setCharacterEncoding("utf-8"); 4 response.setContentType("text/json;charset=utf-8"); 5 //容许全部IP地址和端口请求 6 response.setHeader("Access-Control-Allow-Origin", "*"); 7 //容许全部的文档类型请求 8 response.setHeader("Access-Control-Content-Type", "*"); 9 HashMap<String, String> map = new HashMap<String, String>(); 10 map.put("user", new String(request.getParameter("user").getBytes("ISO-8859-1"),"utf-8")); 11 map.put("psw", new String(request.getParameter("psw").getBytes("ISO-8859-1"),"utf-8")); 12 System.out.println(new String(request.getParameter("user").getBytes("ISO-8859-1"),"utf-8")); 13 String jsonString = JSON.toJSONString(map); 14 response.getWriter().println(jsonString); 15 } 16 17 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.setCharacterEncoding("utf-8"); 18 request.setCharacterEncoding("utf-8"); 19 response.setCharacterEncoding("utf-8"); 20 response.setContentType("text/json;charset=utf-8"); 21 //容许全部IP地址和端口请求 22 response.setHeader("Access-Control-Allow-Origin", "*"); 23 //容许全部的文档类型请求 24 response.setHeader("Access-Control-Content-Type", "*"); 25 HashMap<String, String> map = new HashMap<String, String>(); 26 map.put("user", request.getParameter("user")); 27 map.put("psw", request.getParameter("psw")); 28 System.out.println(request.getParameter("user")); 29 String jsonString = JSON.toJSONString(map); 30 response.getWriter().println(jsonString); 31 }
服务器接收 JSON 并取出数据发回浏览器:
浏览器:
1 $.ajax({ 2 url: 'http://localhost:8080/RequestNResponse/GetJSON', 3 method: 'GET', 4 data: { 5 json: 6 `{ 7 "username": "张三", 8 "age": 23, 9 "hobby":["篮球","足球","羽毛球"] 10 }` 11 }, 12 complete: function (res) { 13 console.log(res) 14 $('body').append(`<h1>${res.responseText}</h1>`) 15 } 16 })
服务器:
1 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 2 request.setCharacterEncoding("utf-8"); 3 response.setCharacterEncoding("utf-8"); 4 response.setContentType("text/html;charset=utf-8"); 5 //容许全部IP地址和端口请求 6 response.setHeader("Access-Control-Allow-Origin", "*"); 7 //容许全部的文档类型请求 8 response.setHeader("Access-Control-Content-Type", "*"); 9 String jsonStr = new String(request.getParameter("json").getBytes("ISO-8859-1"), "utf-8"); 10 JSONObject object = JSON.parseObject(jsonStr); 11 response.getWriter().println("username:" + object.get("username")); 12 response.getWriter().println("username:" + object.get("age")); 13 JSONArray hobbies = (JSONArray) object.get("hobby"); 14 hobbies.forEach(obj -> { 15 try { 16 response.getWriter().println(obj); 17 } catch (IOException e) { 18 // TODO Auto-generated catch block 19 e.printStackTrace(); 20 } 21 }); 22 } 23 24 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 25 request.setCharacterEncoding("utf-8"); 26 response.setCharacterEncoding("utf-8"); 27 response.setContentType("text/html;charset=utf-8"); 28 //容许全部IP地址和端口请求 29 response.setHeader("Access-Control-Allow-Origin", "*"); 30 //容许全部的文档类型请求 31 response.setHeader("Access-Control-Content-Type", "*"); 32 String jsonStr = request.getParameter("json"); 33 JSONObject object = JSON.parseObject(jsonStr); 34 response.getWriter().println("username:" + object.get("username")); 35 response.getWriter().println("username:" + object.get("age")); 36 JSONArray hobbies = (JSONArray) object.get("hobby"); 37 hobbies.forEach(obj -> { 38 try { 39 response.getWriter().println(obj); 40 } catch (IOException e) { 41 // TODO Auto-generated catch block 42 e.printStackTrace(); 43 } 44 }); 45 }