Json詳解html
Json是一種輕量級的數據交換格式,採用一種「鍵:值」對的文本格式來存儲和表示數據,在系統交換數據過程當中經常被使用,是一種理想的數據交換語言。在使用Java作Web開發時,不可避免的會遇到Json的使用。前端
一:JSON形式與語法java
1.1:JSON對象jquery
咱們先來看如下數據:ajax
{json 「ID」: 「1001」,後端 「name」: 「張三」,數組 「age」: 「24」app }框架 |
第一個數據就是一個Json對象,觀察它的數據形式,能夠得出如下語法:
1:數據在花括號中
2:數據以鍵:值對的形式出現(其中鍵多以字符串形式出現,值可取字符串,數值,甚至其餘json對象)
3:每兩個鍵:值對以逗號分隔(最後一個鍵:值對省略逗號)
遵照上面3點,即可以造成一個json對象。
1.2:JSON對象數組
接下來咱們再看第二個數據,觀察它的數據形式,能夠得出如下語法:
[ {"ID": 1001, "name": "張三", "age": 24}, {"ID": 1002, "name": "李四", "age": 25}, {"ID": 1003, "name": "王五", "age": 22} ] |
1:數據在方括號中(可理解爲數組)
2:方括號中每一個數據以json對象形式出現
3:每兩個數據以逗號分隔(最後一個無需逗號)
遵照上面3點,即可造成一個json對象數組(及一個數組中,存儲了多個json對象)
理解了上面兩種基本的形式,咱們就能夠得出其餘的數據形式,例以下面這個:
{ "部門名稱":"研發部", "部門成員":[ {"ID": 1001, "name": "張三", "age": 24}, {"ID": 1002, "name": "李四", "age": 25}, {"ID": 1003, "name": "王五", "age": 22}], "部門位置":"xx樓21號" }
|
這是上面兩個基本形式結合出來的一種變形,經過這種變形,使得數據的封裝具備很大的靈活性,能讓開發者自由的發揮想象力。
總結:json能夠簡單的分爲基本形式:json對象,json對象數組。兩種基本格式組合變形出其餘的形式,但其本質仍是json對象或者json對象數組中的一種。json對象或對象數組能夠轉化爲json字符串,使用於不一樣的場合。
FastJson的介紹
JSON協議使用方便,愈來愈流行,JSON的處理器有不少,這裏我介紹一下FastJson,FastJson是阿里的開源框架,被很多企業使用,是一個極其優秀的Json框架,Github地址: FastJson
1.FastJson的特色
1.FastJson數度快,不管序列化和反序列化,都是當之無愧的fast
2.功能強大(支持普通JDK類包括任意Java Bean Class、Collection、Map、Date或enum)
3.零依賴(沒有依賴其它任何類庫)
2.Fastjson中的常常調用的方法
parse(String text); // 把JSON文本parse爲JSONObject或者JSONArray
parseObject(String text); // 把JSON文本parse成JSONObject
parseArray(String text); // 把JSON文本parse成JSONArray
toJSONString(Object object); // 將JavaBean序列化爲JSON文本
//後端--》前端
package cn.sxt; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.Properties; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.alibaba.fastjson.JSON; /** * Servlet implementation class lianxi2 */ @WebServlet("/lianxi2") public class lianxi2 extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public lianxi2() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/json"); //建立java對象 HashMap<String, Object> hashMap = new HashMap<String,Object>(); hashMap.put("name", "蔡徐坤"); hashMap.put("age","21"); hashMap.put("height","183"); ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("唱"); arrayList.add("跳"); arrayList.add("rap"); arrayList.add("籃球"); hashMap.put("like", arrayList); //將Java對象轉爲字符串 String jsonString = JSON.toJSONString(hashMap); response.getWriter().println(jsonString); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <script src="js/jquery-1.11.0.js"></script> </head> <body> <script> $.get('./lianxi2').then(function(res){ console.log(res) $("body").append(` <h1>${res.name}</h1> <h2>${res.height}</h2> <h3>${res.age}</h3> `) for(var i=0;i<res.like.length;i++){ $("body").append( `<h4>蔡徐坤喜歡的第${i}個內容: ${res.like[i]}</h4>` ) } }) </script> </body> </html>
package cn.sxt; //前端--》後端 import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; /** * Servlet implementation class lianxi3 */ @WebServlet("/lianxi3") public class lianxi3 extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public lianxi3() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/json"); //將JavaBean序列化爲JSON文本 String jsonStr = request.getParameter("json"); response.getWriter().println("成功獲取:"+jsonStr); //把JSON文本parse成JSONObject JSONObject parseObject = JSON.parseObject(jsonStr); System.out.println(parseObject.get("partname")); System.out.println(parseObject.get("position")); Object object = parseObject.get("partman"); System.out.println(object); //把JSON文本parse成JSONArray JSONArray parseArray = JSON.parseArray(object.toString()); System.out.println(parseArray.get(2)); } }
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <script src="js/jquery-1.11.0.js"></script> </head> <body> <script> var data = { json:` { "partname":"研發部", "partman":[ {"ID": 1001, "name": "張三", "age": 24}, {"ID": 1002, "name": "李四", "age": 25}, {"ID": 1003, "name": "王五", "age": 22}], "position":"xx樓21號" }` } $.ajax({ url:"./lianxi3", data:data, method:"POST", complete:function(res){ console.log(res) } }) </script> </body> </html>