從零搭建hadoop叢集之zookeeper叢集安裝
阿新 • • 發佈:2020-12-16
JSON
JavaScript Object Notation,⼀種輕量級資料互動格式,完成 js 與 Java 等後端開發語⾔物件資料之間的轉換。
客戶端和伺服器之間傳遞物件資料,需要用JSON格式。
package com.southwind.entity; public class User { private Integer id; private String name; private Double score; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; }public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getScore() { return score; } public void setScore(Double score) { this.score = score; } public User(Integer id, String name, Double score) { this.id = id; this.name = name; this.score = score; } } User user = new User(1,"張三",96.5);
var user = { id:1, name:"張三", score:96.5 }
package com.southwind.servlet; import com.southwind.entity.User; import net.sf.json.JSONObject; 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 java.io.IOException; @WebServlet("/test") public class TestServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { User user = new User(1,"張三",96.5); //將 Java 物件轉為 JSON 格式 resp.setCharacterEncoding("UTF-8"); JSONObject jsonObject = JSONObject.fromObject(user); resp.getWriter().write(jsonObject.toString()); } }
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> <script type="text/javascript" src="js/jquery-3.3.1.min.js"></script> <script type="text/javascript"> $(function(){ var btn = $("#btn"); btn.click(function(){ $.ajax({ url:'/test', type:'post', dataType:'json', success:function(data){ $("#id").val(data.id); $("#name").val(data.name); $("#score").val(data.score); } }); }); }) </script> </head> <body> 編號:<input id="id" type="text"/><br/> 姓名:<input id="name" type="text"/><br/> 成績:<input id="score" type="text"/><br/> <input id="btn" type="button" value="提交"/> </body> </html>
在開發微信小程式的時候,基本上使用的都是JSON語言
所以看了看JSON的格式以及基本用法