一. 前言
JSON即JavaScript 对象表示法(JavaScript Object Notation),是一个存储和交换文本信息的语法。JSON的数据在都在名称/值对中,
数据由逗号分隔,大括号中保存对象,中括号中保存数组。其中名称/值对都用双引号包裹,比如:”name”:”zhangjia”,相当于Js中的name:'zhangjia'
二. JSON和JS对象的互相转换
1. JS对象转换为JSON字符串
JSON.stringify(js对象)
2. 将JSON字符串转换为JS对象
JSON.parse(json对象)
举例:
<script type="text/javascript"> var js = {name:'zhangjia',age:22}; console.log(js ); console.log("js type = " + typeof js) //将js对象转换为JSON字符串 var json = JSON.stringify(js); console.log(json); console.log("json type = " + typeof json) //将JSON对象转换为JS json = JSON.parse(json); console.log(json); console.log("json type = " + typeof json) /*输出 {name: "zhangjia", age: 22} js type = object {"name":"zhangjia","age":22} json type = string {name: "zhangjia", age: 22} json type = object */ </script>
三. JAVA对象和JSON对象互相转换
我们可以使用fastjson来完成 JAVA对象和JSON对象互相转换。
1. Java对象转JSON
JSON.toJSONString(java对象);
2. JSON转Java对象
JSON.parseObject(json对象,X.class); //返回的是x类的对象 JSON.parseObject(json对象); //返回的是JSONObject对象,可以用map的方式获取数据
举例:
package util; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; public class Json { public static void main(String[] args) { //Java对象转JSON A a = new A("zhangjia",22); System.out.println(a); String json = JSON.toJSONString(a); System.out.println(json); //JSON对象转Java //原数据是{"age":22,"name":"zhangjia"},因为放在了""中,所以每个"前加了\转义 String json2 = "{\"age\":22,\"name\":\"zhangjia\"}"; //方法一:A类中必须有规范的settter方法才可以查询到 A b = JSON.parseObject(json2,A.class); System.out.println(b.getName()); //方法二: JSONObject c = JSON.parseObject(json2); System.out.println(c); System.out.println(c.get("name")); } } class A { private String name; private int age; public String getName() { return name; } public int getAge() { return age; } public A() { } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public A(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "A{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
四. Servlet返回JSON格式的字符串
1. contentType
contentType意为内容类型用于定义网络文件的类型和网页的编码,它决定了浏览器将以什么形式、什么编码读取文件,这就是经常看到一些Asp网页点击的结果却是下载到的一个文件或一张图片的原因。我们可以打开Tomcat的安装目录:Tomacat\apache-tomcat-8.5.41\conf\web.xml文件查询每种文件类型对应的contentType
2. Servlet客户端响应字符串
我们在使用Servlet向客户端响应一个字符时,一定要设置contentType,并指定编码,否则当参数为中文时,会产生乱码。
package servlet; import com.alibaba.fastjson.JSON; import dao.BikeDao; import dao.impl.BikeDaoImpl; import entity.Bike; 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; import java.io.PrintWriter; @WebServlet("/json") public class JSONServlet extends HttpServlet { private BikeDao bikeDao = new BikeDaoImpl(); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String name = req.getParameter("name"); //resp.setContentType("text/html; charset=utf-8"); PrintWriter writer = resp.getWriter(); writer.println(name); writer.close(); } }
将上面的代码中的resp.setContentType注释后再访问http://localhost:8888/svt/json?name=zhangjia,页面显示zhangjia,而如果访问http://localhost:8888/svt/json?name=张甲,页面就会显示乱码。
3. 使用Servlet返回JSON格式的字符串
我们可以在Servlet中将查询的对象以JSON的格式返回:
package servlet; import com.alibaba.fastjson.JSON; import dao.BikeDao; import dao.impl.BikeDaoImpl; import entity.Bike; 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; import java.io.PrintWriter; @WebServlet("/json") public class JSONServlet extends HttpServlet { private BikeDao bikeDao = new BikeDaoImpl(); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String id = req.getParameter("id"); Bike bike = null; if(id != null) { bike = bikeDao.queryById(Integer.parseInt(id)); } resp.setContentType("application/json; charset=utf-8"); PrintWriter writer = resp.getWriter(); writer.println(JSON.toJSONString(bike)); writer.close(); } }
请登录之后再进行评论