Jackson可以轻松的将Java对象转换成json对象和xml文档,同样也可以将json、xml转换成Java对象。
1.创建动态web工程。
2.编写modle类
package org.itat.zttc.model; public class Person { private int id; private String name; private double salary; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Person(int id,String name,double salary,int age) { super(); this.id = id; this.name = name; this.salary = salary; this.age = age; } public Person() { } }
package org.itat.zttc.model; import java.util.ArrayList; import java.util.List; public class Group { private int id; private String name; List<User> users; public void addUser(User u) { if(users==null) { users = new ArrayList<User>(); } users.add(u); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Group(int id,String name) { super(); this.id = id; this.name = name; } public Group() { } }
package org.itat.zttc.model; public class User { private int id; private String username; private String password; private Group group; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Group getGroup() { return group; } public void setGroup(Group group) { this.group = group; } public User() { } public User(int id,String username,String password,Group group) { super(); this.id = id; this.username = username; this.password = password; this.group = group; } public User(int id,String password) { super(); this.id = id; this.username = username; this.password = password; } }
3.导入jackson包
4.编写测试类
package org.itat.zttc.test; import java.io.IOException; import java.io.StringWriter; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.itat.zttc.model.Group; import org.itat.zttc.model.User; import org.itat.zttc.util.JsonUtil; import org.junit.Test; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; public class TestJaskson { @Test public void test01() { StringWriter out = new StringWriter(); JsonGenerator jg = null; try { //1、创建JsonFactory JsonFactory jf = new JsonFactory(); //2、创建JsonGenerator jg = jf.createJsonGenerator(out); //使用一种相对漂亮的格式输出 jg.useDefaultPrettyPrinter(); //3、创建ObjectMapper,通过ObjectMapper来写对象 User u = new User(1,"劳动法","123",new Group(1,"财务处")); ObjectMapper mapper = new ObjectMapper(); mapper.writeValue(jg,u); System.out.println(out.toString()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if(jg!=null) jg.close(); } catch (IOException e) { e.printStackTrace(); } } } @Test public void test02() { StringWriter out = new StringWriter(); JsonGenerator jg = null; try { //1、创建JsonFactory JsonFactory jf = new JsonFactory(); //2、创建JsonGenerator jg = jf.createJsonGenerator(out); //使用一种相对漂亮的格式输出 // jg.useDefaultPrettyPrinter(); //3、创建ObjectMapper,通过ObjectMapper来写对象 List<User> us = new ArrayList<User>(); User u = new User(1,"财务处")); us.add(u); u = new User(2,"猪八戒","财务处")); us.add(u); u = new User(3,"张学友",new Group(2,"教务处")); us.add(u); ObjectMapper mapper = new ObjectMapper(); mapper.writeValue(jg,us); System.out.println(out.toString()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if(jg!=null) jg.close(); } catch (IOException e) { e.printStackTrace(); } } } @Test public void test02_1() { List<User> us = new ArrayList<User>(); User u = new User(1,"财务处")); us.add(u); u = new User(2,"财务处")); us.add(u); u = new User(3,"教务处")); us.add(u); System.out.println(JsonUtil.getInstance().obj2json(us)); } @Test public void test03() { try { String json = "{\"id\":1,\"username\":\"杂货\",\"password\":\"123\",\"group\":{\"id\":1,\"name\":\"财务处\"}}"; ObjectMapper mapper = new ObjectMapper(); User u = mapper.readValue(json,User.class); System.out.println(u.getId()+","+u.getUsername()+","+u.getGroup().getName()); } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } @Test public void test04() { try { String json = "[{\"id\":1,\"username\":\"劳动法\",\"name\":\"财务处\"}},{\"id\":2,\"username\":\"猪八戒\",{\"id\":3,\"username\":\"张学友\",\"group\":{\"id\":2,\"name\":\"教务处\"}}]"; ObjectMapper mapper = new ObjectMapper(); //在把json转换为list的时候,不能直接存储Bean对象,list中存储的是Map对象 /*List<User> us = mapper.readValue(json,List.class); for(User u:us) { System.out.println(u.getUsername()); }*/ /** * 所以的json都是通过map来存储的,不会直接存储bean,但是在开发中,把字符串转换为对象 * 一般只会对单个的对象转换,很少会用到对对象的完全转换 */ List<Map<String,Object>> us = mapper.readValue(json,List.class); for(Map<String,Object> m:us) { System.out.println(m.get("id")); System.out.println(m.get("group")); } } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } @Test public void test05() { try { String json = "[{\"id\":1,\"name\":\"教务处\"}}]"; ObjectMapper mapper = new ObjectMapper(); /* * 在Jaskson中提供了一种基于节点的读取方法 */ JsonNode node = mapper.readTree(json); //判断这个节点是否是数组 System.out.println(node.isArray()); System.out.println(node.size()); System.out.println(node.get(0).get("username")); System.out.println(node.get(0).get("group").get("name")); } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
5.编写工具类。
package org.itat.zttc.util; import java.io.IOException; import java.io.StringWriter; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; public class JsonUtil { private static JsonUtil ju; private static JsonFactory jf; private static ObjectMapper mapper; private JsonUtil(){} public static JsonUtil getInstance() { if(ju==null) ju = new JsonUtil(); return ju; } public static ObjectMapper getMapper() { if(mapper==null) { mapper = new ObjectMapper(); } return mapper; } public static JsonFactory getFactory() { if(jf==null) jf = new JsonFactory(); return jf; } public String obj2json(Object obj) { JsonGenerator jg = null; try { jf = getFactory(); mapper = getMapper(); StringWriter out = new StringWriter(); jg = jf.createJsonGenerator(out); mapper.writeValue(jg,obj); return out.toString(); } catch (IOException e) { e.printStackTrace(); } finally { try { if(jg!=null) jg.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } public Object json2obj(String json,Class<?> clz) { try { mapper = getMapper(); return mapper.readValue(json,clz); } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } }6.运行程序,察看运行结果 原文链接:https://www.f2er.com/ajax/163847.html