发布时间:2022-07-05 文章分类:编程知识 投稿人:赵颖 字号: 默认 | | 超大 打印

jackson处理json对象相关小结

    博客分类:

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

1 JAVA 对象转为JSON

Java代码
  1. importjava.io.File;
  2. importjava.io.IOException;
  3. importorg.codehaus.jackson.JsonGenerationException;
  4. importorg.codehaus.jackson.map.JsonMappingException;
  5. importorg.codehaus.jackson.map.ObjectMapper;
  6. publicclassJacksonExample{
  7. publicstaticvoidmain(String[]args){
  8. Useruser=newUser();
  9. ObjectMappermapper=newObjectMapper();
  10. try{
  11. //convertuserobjecttojsonstring,andsavetoafile
  12. mapper.writeValue(newFile("c:\\user.json"),user);
  13. //displaytoconsole
  14. System.out.println(mapper.writeValueAsString(user));
  15. }catch(JsonGenerationExceptione){
  16. e.printStackTrace();
  17. }catch(JsonMappingExceptione){
  18. e.printStackTrace();
  19. }catch(IOExceptione){
  20. e.printStackTrace();
  21. }
  22. }
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对象

Java代码
  1. importjava.io.File;
  2. importjava.io.IOException;
  3. importorg.codehaus.jackson.JsonGenerationException;
  4. importorg.codehaus.jackson.map.JsonMappingException;
  5. importorg.codehaus.jackson.map.ObjectMapper;
  6. publicclassJacksonExample{
  7. publicstaticvoidmain(String[]args){
  8. ObjectMappermapper=newObjectMapper();
  9. try{
  10. //readfromfile,convertittouserclass
  11. Useruser=mapper.readValue(newFile("c:\\user.json"),User.class);
  12. //displaytoconsole
  13. System.out.println(user);
  14. }catch(JsonGenerationExceptione){
  15. e.printStackTrace();
  16. }catch(JsonMappingExceptione){
  17. e.printStackTrace();
  18. }catch(IOExceptione){
  19. e.printStackTrace();
  20. }
  21. }
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()方法,例子为:

Java代码
  1. Useruser=newUser();
  2. ObjectMappermapper=newObjectMapper();
  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"

}