Jackson简单小结
- - jackyrong 本文简单小结下 jackson的常用操作 ,使用的是1.9.13的版本;. 1) 简单pojo转换json:. 如果要转换为良好的格式,则:. 2 接下来看如何把hashmap转换为json:. 再把hashmap反转为json. 已有 0 人发表留言,猛击->> 这里<<-参与讨论.
public class Employee { private Integer id; private String firstName; private String lastName; public Employee(){ } public Employee(Integer id, String firstName, String lastName, Date birthDate){ this.id = id; this.firstName = firstName; this.lastName = lastName; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } @Override public String toString() { return "Employee [id=" + id + ", firstName=" + firstName + ", " + "lastName=" + lastName + "]"; } }
public class JavaToJSONExample { public static void main(String[] args) { @SuppressWarnings("deprecation") Employee employee = new Employee(1, "Lokesh", "Gupta", new Date(1981,8,18)); ObjectMapper mapper = new ObjectMapper(); try { mapper.writeValue(new File("c://temp/employee.json"), employee); } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } Output: //In employee.json file below content will be written {"id":1,"firstName":"Lokesh","lastName":"Gupta"}
public class JavaToPrettyJSONExample { public static void main(String[] args) { @SuppressWarnings("deprecation") Employee employee = new Employee(1, "Lokesh", "Gupta", new Date(1981,8,18)); ObjectMapper mapper = new ObjectMapper(); try { mapper.defaultPrettyPrintingWriter().writeValue(new File("c://temp/employee.json"), employee); } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } Output: { "id" : 1, "firstName" : "Lokesh", "lastName" : "Gupta" }
public class JSONToJavaExample { public static void main(String[] args) { Employee employee = null; ObjectMapper mapper = new ObjectMapper(); try { employee = mapper.readValue(new File("c://temp/employee.json"), Employee.class); } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println(employee); } } Output: Employee [id=1, firstName=Lokesh, lastName=Gupta]
public class JavaToHashMapExample { public static void main(String[] args) { HashMap<string, object=""> hashmap = new HashMap<string, object="">(); hashmap.put("id", 11); hashmap.put("firstName", "Lokesh"); hashmap.put("lastName", "Gupta"); ObjectMapper mapper = new ObjectMapper(); try { mapper.writeValue(new File("c://temp/data.json"), hashmap); } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } File content: {"id":11,"lastName":"Gupta","firstName":"Lokesh"}
public class HashMapToJSONExample { public static void main(String[] args) { HashMap<string, object=""> hashmap = new HashMap<string, object="">(); ObjectMapper mapper = new ObjectMapper(); try { hashmap = mapper.readValue(new File("c://temp/data.json"), new TypeReference<map<string, object="">>(){ }); } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println(hashmap); } } Output: {id=11, lastName=Gupta, firstName=Lokesh}