Java 進階 & JackJson 的使用

需要包:
jackson-core-2.2.3.jar(核心jar包)
jackson-annotations-2.2.3.jar(該包提供Json注解支持)
jackson-databind-2.2.3.jar

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.9.1</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.9.1</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.1</version>
</dependency>
1、指定對象Class轉成 json字符串
User user = new User();
user.setName("小民");
user.setEmail("xiaomin@sina.com");
user.setAge(20);

SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
user.setBirthday(dateformat.parse("1996-10-01"));

/**

  • ObjectMapper是JSON操作的核心,Jackson的所有JSON操作都是在ObjectMapper中實現。
  • ObjectMapper有多個JSON序列化的方法,可以把JSON字符串保存File、OutputStream等不同的介質中。
  • writeValue(File arg0, Object arg1)把arg1轉成json序列,并保存到arg0文件中。
  • writeValue(OutputStream arg0, Object arg1)把arg1轉成json序列,并保存到arg0輸出流中。
  • writeValueAsBytes(Object arg0)把arg0轉成json序列,并把結果輸出成字節數組。
  • writeValueAsString(Object arg0)把arg0轉成json序列,并把結果輸出成字符串。
    */
    ObjectMapper mapper = new ObjectMapper();

//User類轉JSON

{"name":"小民","age":20,"birthday":844099200000,"email":"xiaomin@sina.com"}
String json = mapper.writeValueAsString(user);
System.out.println(json);

輸出:
{"name":"小民","age":20,"birthday":844099200000,"email":"xiaomin@sina.com"}

2、List集合轉化成json字符串
List<User> users = new ArrayList<User>();
users.add(user);
String jsonlist = mapper.writeValueAsString(users);
System.out.println(jsonlist);

輸出:

[{"name":"小民","age":20,"birthday":844099200000,"email":"xiaomin@sina.com"}]
3、Json字符串轉化成指定Class類
String json = "{\"name\":\"小民\",\"age\":20,\"birthday\":844099200000,\"email\":\"xiaomin@sina.com\"}";
/**
 * ObjectMapper支持從byte[]、File、InputStream、字符串等數據的JSON反序列化。
 */
ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(json, User.class);
System.out.println(user);

輸出:

User{name='小民aa', age=25, birthday=Tue Oct 01 00:00:00 CST 1996, email='xiaomin@sina.com'}
4、Json字符串轉化成集合List

方法一:

String jsonString="[{'id':'1'},{'id':'2'}]";
ObjectMapper mapper = new ObjectMapper();
JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, Bean.class);
//如果是Map類型  mapper.getTypeFactory().constructParametricType(HashMap.class,String.class, Bean.class);  
List<Bean> lst =  (List<Bean>)mapper.readValue(jsonString, javaType);

輸出:

Student{name='s1', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s2', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s3', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s4', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s5', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s6', age=12, date=Thu Jun 09 20:38:37 CST 2016}

方法二:

String jsonString="[{'id':'1'},{'id':'2'}]";
ObjectMapper mapper = new ObjectMapper();
List<Bean> beanList = mapper.readValue(jsonString, new TypeReference<List<Bean>>() {});

輸出:

Student{name='s1', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s2', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s3', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s4', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s5', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s6', age=12, date=Thu Jun 09 20:38:37 CST 2016}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Jackson使用規范以及代碼示例 依賴包 Maven依賴: org.codehaus.jackson jacks...
    山石水壽閱讀 4,755評論 0 3
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,993評論 19 139
  • Spring Boot 參考指南 介紹 轉載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,973評論 6 342
  • 她全神貫注的盯著眼前的小珠子,然后一顆顆的將不同的顏色分類,放好,然后拿起細繩一點點往珠子的洞眼里穿。剛開始有點費...
    莉莉安蓁妮閱讀 384評論 0 0
  • 最近看了日本史學家宮崎市定先生的《中國史》,閱讀的過程有過一次小的糾結,即讀著讀著甚至忘記了真正的歷史是什么樣了,...
    慧飛的魚閱讀 475評論 0 0