0
点赞
收藏
分享

微信扫一扫

jackson处理json对象相关小结


在解析JSON方面,无疑JACKSON是做的最好的,下面从几个方面简单复习下。

1 JAVA 对象转为JSON
 


    1.   
    2. import
    3. import
    4. import
    5. import
    6. import
    7.
    8. public class
    9. public static void
    10.
    11. new
    12. new
    13.
    14. try
    15.
    16. // convert user object to json string, and save to a file
    17. new File("c:\\user.json"), user);
    18.
    19. // display to console
    20. System.out.println(mapper.writeValueAsString(user));
    21.
    22. catch
    23.
    24. e.printStackTrace();
    25.
    26. catch
    27.
    28. e.printStackTrace();
    29.
    30. catch
    31.
    32. e.printStackTrace();
    33.
    34. }
    35.
    36. }
    37.


    import java.io.File;
    import java.io.IOException;
    import org.codehaus.jackson.JsonGenerationException;
    import org.codehaus.jackson.map.JsonMappingException;
    import org.codehaus.jackson.map.ObjectMapper;

    public class JacksonExample {
    public static void main(String[] args) {

    User user = new User();
    ObjectMapper mapper = new ObjectMapper();

    try {

    // convert user object to json string, and save to a file
    mapper.writeValue(new File("c:\\user.json"), user);

    // display to console
    System.out.println(mapper.writeValueAsString(user));

    } catch (JsonGenerationException e) {

    e.printStackTrace();

    } catch (JsonMappingException e) {

    e.printStackTrace();

    } catch (IOException e) {

    e.printStackTrace();

    }

    }



    输出为:


    {"age":29,"messages":["msg 1","msg 2","msg 3"],"name":"mkyong"}





    2 JSON反序列化为JAVA对象




      1. import
      2. import
      3. import
      4. import
      5. import
      6.
      7. public class
      8. public static void
      9.
      10. new
      11.
      12. try
      13.
      14. // read from file, convert it to user class
      15. new File("c:\\user.json"), User.class);
      16.
      17. // display to console
      18. System.out.println(user);
      19.
      20. catch
      21.
      22. e.printStackTrace();
      23.
      24. catch
      25.
      26. e.printStackTrace();
      27.
      28. catch
      29.
      30. e.printStackTrace();
      31.
      32. }
      33.
      34. }
      35.

      import java.io.File;
      import java.io.IOException;
      import org.codehaus.jackson.JsonGenerationException;
      import org.codehaus.jackson.map.JsonMappingException;
      import org.codehaus.jackson.map.ObjectMapper;

      public class JacksonExample {
      public static void main(String[] args) {

      ObjectMapper mapper = new ObjectMapper();

      try {

      // read from file, convert it to user class
      User user = mapper.readValue(new File("c:\\user.json"), User.class);

      // display to console
      System.out.println(user);

      } catch (JsonGenerationException e) {

      e.printStackTrace();

      } catch (JsonMappingException e) {

      e.printStackTrace();

      } catch (IOException e) {

      e.printStackTrace();

      }

      }

      import java.io.File;
      import java.io.IOException;
      import org.codehaus.jackson.JsonGenerationException;
      import org.codehaus.jackson.map.JsonMappingException;
      import org.codehaus.jackson.map.ObjectMapper;

      public class JacksonExample {
      public static void main(String[] args) {

      ObjectMapper mapper = new ObjectMapper();

      try {

      // read from file, convert it to user class
      User user = mapper.readValue(new File("c:\\user.json"), User.class);

      // display to console
      System.out.println(user);

      } catch (JsonGenerationException e) {

      e.printStackTrace();

      } catch (JsonMappingException e) {

      e.printStackTrace();

      } catch (IOException e) {

      e.printStackTrace();

      }

      }


      输出:User [age=29, name=mkyong, messages=[msg 1, msg 2, msg 3]]



      3 在上面的例子中,如果要输出的JSON好看点,还是有办法的,就是使用


      defaultPrettyPrintingWriter()方法,例子为:


      ​​​ ​​


      1. User user = new
      2. new
      3. System.out.println(mapper.defaultPrettyPrintingWriter().writeValueAsString(user));


      User user = new User();
      ObjectMapper mapper = new ObjectMapper();
      System.out.println(mapper.defaultPrettyPrintingWriter().writeValueAsString(user));



      则输出整齐:


      { 

      "age" : 29,

      "messages" : [ "msg 1", "msg 2", "msg 3" ],

      "name" : "mkyong"

      }

      举报

      相关推荐

      0 条评论