XStream的应用
- 作用:将JavaBean转换成(序列化)xml
- jar包:xstream-1.4.10.jar;xpp3_min-1.1.4c.jar
- 如何使用:
1)建立好JavaBean模型
2)XStream xstream = new XStream(); String xml = xstream.toXML(JavaBean对象); - 其它方法:
1)xstream.alias(“要替换后的标签名称”,xxx.class); 将该类的标签名称进行替换
2)xstream.omitField(xxx.class,“元素名称”) 将该元素的名称进行去除,不生成xml
3)xstream.useAttributeFor(xxx.class,“属性名称”); 将该类下的元素变为自己的属性生成xml
4)xstream.addImplicitCollection(xxx.class,“标签名”); 去除Collection类型的成员,只需要它的内容,不需要它本身
package xstream; public class City { private String name; private int code; private String description; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public City(String name, int code, String description) { super(); this.name = name; this.code = code; this.description = description; } public City() { super(); } }
package xstream; import java.util.List; public class Province { private String name; private List<City> cities; public String getName() { return name; } public void setProvince(String name) { this.name = name; } public List<City> getCities() { return cities; } public void setCities(List<City> cities) { this.cities = cities; } public Province(String name, List<City> cities) { super(); this.name = name; this.cities = cities; } public Province() { super(); } }
package xstream; import java.util.ArrayList; import java.util.List; import org.junit.Test; import com.thoughtworks.xstream.XStream; public class Test1 { public List<Province> getProvince(){ Province p1 = new Province(); Province p2 = new Province(); p1.setProvince("陕西"); List<City> list1 = new ArrayList<City>(); list1.add(new City("西安", 001, "Xi'an")); list1.add(new City("榆林", 002, "YuLin")); p1.setCities(list1); p2.setProvince("宁夏"); List<City> list2 = new ArrayList<City>(); list2.add(new City("银川", 001, "YinChuan")); list2.add(new City("吴忠", 002, "WuZhong")); p2.setCities(list2); List<Province> p = new ArrayList<Province>(); p.add(p1); p.add(p2); return p; } @Test public void fun(){ List<Province> province = this.getProvince(); XStream xstream = new XStream(); xstream.alias("china", List.class); xstream.alias("province", Province.class); xstream.alias("city", City.class); xstream.useAttributeFor(Province.class, "name"); xstream.omitField(City.class, "description"); xstream.addImplicitCollection(Province.class, "cities"); String xml = xstream.toXML(province); System.out.println(xml); } }
JSON的应用
- 语法:
1)对象用{ }括起来
2)属性名必须用双引号括起来" ",单引号不可以
3)属性值:null、数值、字符串、数组(用[ ]括起来)boolean值(true、false) - 举例:
[{“username”: “tqb”, “password”: “password”}, {“address”: “shaan”}] - 应用:
package tqb.servlet; 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; @WebServlet("/AServlet") public class AServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); String json = "{\"province\": \"shaanxi\", \"city\": \"xian\"}"; response.getWriter().print(json); } }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <script type="text/javascript"> function createXMLHttpRequest(){ try{ return new XMLHttpRequest(); }catch(e){ try{ return ActvieXObject("Msxml2.XMLHTTP"); }catch(e){ try{ return ActiveXObject("Microsoft.XMLHTTP"); }catch(e){ alert("您的浏览器版本过低或不兼容,请升级或更换浏览器!!!"); throw e; } } } } window.onload = function(){ var btn = document.getElementById("btn"); btn.onclick = function(){ var xmlHttp = createXMLHttpRequest(); xmlHttp.open("GET", "<c:url value='/AServlet'/>", true); xmlHttp.send(null); xmlHttp.onreadystatechange = function(){ if(xmlHttp.readyState == 4 && xmlHttp.status == 200){ var jsonText = xmlHttp.responseText; var provinces = eval("(" + jsonText + ")"); var h1 = document.getElementById("h1"); h1.innerHTML = provinces.province + "," + provinces.city; } }; }; }; </script> <body> <h1>JSON</h1> <br> <button id="btn">提交</button> <br> <h1 id="h1"></h1> </body> </html>
JSON-lib的应用
- 作用:将对象转换JavaBean转换成JSON
- JSONObject类:
方法:
toString():将生成的json转换成字符串
fromObject():将对象转换成JSONObject对象 - JSONArray类:
方法:
toString():将生成的json转换成字符串
fromObject():将list转换成JSONArray对象
package tqb.servlet; import java.util.ArrayList; import java.util.List; import org.junit.Test; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class Json_lib { @Test public void fun1(){ JSONObject map = new JSONObject(); map.put("name", "zhangsan"); map.put("age", 20); map.put("sex", "male"); String str = map.toString(); System.out.println(str); } @Test public void fun2(){ Person p = new Person("lisi", 21, "female"); JSONObject map = JSONObject.fromObject(p); System.out.println(map.toString()); } @Test public void fun3(){ Person p1 = new Person("lisi", 21, "female"); Person p2 = new Person("zhangsan", 20, "male"); JSONArray list = new JSONArray(); list.add(p1); list.add(p2); String str = list.toString(); System.out.println(str); } @Test public void fun4(){ List<Person> list = new ArrayList<Person>(); Person p1 = new Person("lisi", 21, "female"); Person p2 = new Person("zhangsan", 20, "male"); list.add(p1); list.add(p2); JSONArray li = JSONArray.fromObject(list); System.out.println(li.toString()); } }