Gson介绍:
Gson 是 Google 提供的用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库。可以将一个 JSON 字符串转成一个 Java 对象,或者反过来。
示例代码:
Gson gson = new Gson();
int[] ints = {1, 2, 3, 4, 5};
String[] strings = {"a", "b", "c"};
(Serialization)
gson.toJson(ints); ==> prints [1,2,3,4,5]
gson.toJson(strings); ==> prints ["a", "b", "c"]
下载地址
下载完成需要进行jar包导入;
实例:
工具类 用来生成json格式数据
public class JsonTools { /** * 使用Gson生成json数据 * @param object * @return */ public static String createJson(Object object){ Gson gson = new Gson(); return gson.toJson(object); } }
public class Service { /** * 返回一个Person * @return */ public Person getPerson() { Person person = new Person(); person.setId(1); person.setName("aa"); person.setAddress("beij"); return person; } /** * 返回一组person * @return */ public List<Person> getPersons(){ List<Person> persons = new ArrayList<Person>(); Person person1 = new Person(2,"bb","tianjin"); Person person2 = new Person(3,"cc","beijing"); persons.add(person1); persons.add(person2); return persons; } /** * 返回 List 里面放入Map map里放入person * @return */ public List<Map<String, Object>> getListMap(){ List<Map<String, Object>> list = new ArrayList<Map<String,Object>>(); Map<String, Object> map1 = new HashMap<String, Object>(); map1.put("id", "01"); map1.put("name", "01"); map1.put("address", "01"); Map<String, Object> map2 = new HashMap<String, Object>(); map2.put("id", "02"); map2.put("name", "032"); map2.put("address", "31"); list.add(map1); list.add(map2); return list; } public List<String> getListStr(){ List<String> list = new ArrayList<String>(); list.add("a"); list.add("b"); return list; } }单元测试
public class JsonTest { @Test public void testJson(){ String jsonStr = JsonTools.createJson(new Service().getPerson()); System.out.println(jsonStr); } @Test public void testPersons(){ Service service = new Service(); String str = JsonTools.createJson(service.getPersons()); System.out.println(str); } @Test public void testListMap(){ Service service = new Service(); String str = JsonTools.createJson(service.getListMap()); System.out.println(str); } @Test public void testListStr(){ Service service = new Service(); String str = JsonTools.createJson(service.getListStr()); System.out.println(str); } }
运行结果
testJson()
{"id":1,"name":"aa","address":"beij"}
testPersons()
[{"id":2,"name":"bb","address":"tianjin"},{"id":3,"name":"cc","address":"beijing"}]
testListmap()
[{"id":"01","address":"01","name":"01"},{"id":"02","address":"31","name":"032"}]
testListStr()
["a","b"]
Servlet 响应:
部署到tomcat服务器下,配置web.xml文件来实现多个请求公用一个servlet 请求
方法:1.修改url-pattern
<servlet-mapping> <servlet-name>JsonAction</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>2.在servlet中编写代码
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setCharacterEncoding("utf-8"); request.setCharacterEncoding("utf-8"); // 获取servlet路径名称 String servletPath = request.getServletPath(); // 获取方法 String methodName = servletPath.substring(1); methodName = methodName.substring(0, methodName.length() - 3); try { Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class); method.invoke(this, request, response); } catch (Exception e) { e.printStackTrace(); } }
/** * 得到一个person * @param request * @param response */ private void getPerson(HttpServletRequest request,HttpServletResponse response){ try { PrintWriter out = response.getWriter(); out.println(JsonTools.createJson(service.getPerson())); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 得到一组psrson * @param request * @param response */ private void getPersons(HttpServletRequest request,HttpServletResponse response){ try { PrintWriter out = response.getWriter(); out.println(JsonTools.createJson(service.getPersons())); out.flush(); out.close(); } catch (Exception e) { // TODO: handle exception } } private void getListMap(HttpServletRequest request,HttpServletResponse response){ try { PrintWriter out = response.getWriter(); out.println(JsonTools.createJson(service.getListMap())); out.flush(); out.close(); } catch (Exception e) { // TODO: handle exception } } private void getListStr(HttpServletRequest request,HttpServletResponse response){ try { PrintWriter out = response.getWriter(); out.println(JsonTools.createJson(service.getListStr())); out.flush(); out.close(); } catch (Exception e) { // TODO: handle exception } } //初始化 public void init() throws ServletException { service = new Service(); }实现如下效果
这样就可以用客户端请求网址,得到josn数据,在进行json解析操作
关注我的百度网盘最新资源下载
http://pan.baidu.com/share/home?uk=3641435771
源码下载