Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. package jackson.jacksonTesting;
  2.  
  3. import java.io.IOException;
  4.  
  5. import org.codehaus.jackson.JsonGenerationException;
  6. import org.codehaus.jackson.JsonParseException;
  7. import org.codehaus.jackson.map.JsonMappingException;
  8. import org.codehaus.jackson.map.ObjectMapper;
  9. import org.codehaus.jackson.map.SerializationConfig;
  10.  
  11. public class JacksonTester {
  12. public static void main(String args[]) throws JsonGenerationException, JsonMappingException, IOException
  13. {
  14.  
  15. ObjectMapper mapper = new ObjectMapper();
  16. String jsonString = "";
  17.  
  18. Student s = new Student();
  19. s.setName("mahesh");
  20. s.setAge(22);
  21.  
  22.  
  23.  
  24. mapper.enable(SerializationConfig.Feature.INDENT_OUTPUT);
  25. jsonString = mapper.writeValueAsString(s);
  26. System.out.println(jsonString);
  27.  
  28. s = mapper.readValue(jsonString, Student.class);
  29. System.out.println(s);
  30.  
  31.  
  32.  
  33.  
  34. }
  35. }
  36.  
  37. class Student {
  38. private String name;
  39. private int age;
  40.  
  41. public Student(){}
  42.  
  43. public String getName() {
  44. return name;
  45. }
  46.  
  47. public void setName(String name) {
  48. this.name = name;
  49. }
  50.  
  51. public int getAge() {
  52. return age;
  53. }
  54.  
  55. public void setAge(int age) {
  56. this.age = age;
  57. }
  58. public String toString(){
  59. return "Student [ name: "+name+", age: "+ age+ " ]";
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement