舉例講解Java的Jackson庫(kù)中ObjectMapper類(lèi)的使用
ObjectMapper類(lèi)是Jackson庫(kù)的主要類(lèi)。它提供一些功能將轉(zhuǎn)換成Java對(duì)象匹配JSON結(jié)構(gòu),反之亦然。它使用JsonParser和JsonGenerator的實(shí)例實(shí)現(xiàn)JSON實(shí)際的讀/寫(xiě)。
類(lèi)聲明
以下是org.codehaus.jackson.map.ObjectMapper類(lèi)的聲明:
public class ObjectMapper extends ObjectCodec implements Versioned
嵌套類(lèi)
S.N. | 類(lèi) & 描述 |
---|---|
1 | static class ObjectMapper.DefaultTypeResolverBuilder//定制TypeResolverBuilder,提供所謂的“默認(rèn)輸入”使用類(lèi)型解析構(gòu)建器(見(jiàn)enableDefaultTyping()了解詳細(xì)信息)。 |
2 | static class ObjectMapper.DefaultTyping//使用enableDefaultTyping()枚舉指定什么樣的類(lèi)型(類(lèi))默認(rèn)輸入應(yīng)該使用。 |
構(gòu)造函數(shù)
S.N. | 構(gòu)造函數(shù) & 描述 |
---|---|
1 | ObjectMapper()//默認(rèn)的構(gòu)造函數(shù),這將構(gòu)建默認(rèn)JsonFactory必要時(shí)使用StdSerializerProvider作為其SerializerProvider,并BeanSerializerFactory作為其SerializerFactory。 |
2 | ObjectMapper(JsonFactory jf)//構(gòu)造使用指定的JsonFactory構(gòu)建必要的JsonParsers和/或JsonGenerators映射。 |
3 | ObjectMapper(JsonFactory jf, SerializerProvider sp, DeserializerProvider dp) |
4 | ObjectMapper(JsonFactory jf, SerializerProvider sp, DeserializerProvider dp, SerializationConfig sconfig, DeserializationConfig dconfig) |
5 | ObjectMapper(SerializerFactory sf) 不推薦使用。使用其他構(gòu)造來(lái)代替; 注意,可以設(shè)置序列化工廠setSerializerFactory(org.codehaus.jackson.map.SerializerFactory) |
這個(gè)類(lèi)繼承了以下類(lèi)方法:
java.lang.Object
例子
測(cè)試類(lèi)基本代碼如下
/* * @project java * @package * @file Jackson.java * @version 1.0 */ public class Jackson { /* * * Class Descripton goes here. * * @class Jackson * @version 1.0 */ public static JsonGenerator jsonGenerator = null; private static ObjectMapper mapper = new ObjectMapper(); public static void main(String[] args) { Student student = new Student(); student.setIsstudent(true); student.setUid(1000); student.setUname("xiao liao"); student.setUpwd("123"); student.setNumber(12); Map<String, Student> stuMap = new HashMap<String, Student>(); stuMap.put("1", student); stuMap.put("2", student); List<Object> stuList = new ArrayList<Object>(); List<Student> stuList1 = new ArrayList<Student>(); stuList1.add(student); student= new Student(); student.setIsstudent(false); student.setUid(200); student.setUname("xiao mi"); stuList1.add(student); stuList.add(student); stuList.add("xiao xin"); stuList.add("xiao er"); stuList.add(stuMap); //readJson2List(); try { //readJson2Array(); //writeArray2Json(array); //writeJson2List(); //writeEntity2Json(student); writeJson2Entity(); //writeMap2Json(stuMap); //writeList2Json(stuList1); } catch (IOException e) { e.printStackTrace(); } } /** * * <code>writeEntity2Json</code> * @description: TODO(實(shí)體類(lèi)轉(zhuǎn)換成json) * @param object * @throws IOException */ public static void writeEntity2Json(Object object) throws IOException { mapper.writeValue( new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aa.txt"),object ); mapper.writeValue( System.out,object ); } /** * * <code>writeArray2Json</code> * @description: TODO(數(shù)組轉(zhuǎn)換成json數(shù)組) * @param object * @throws IOException */ public static void writeArray2Json(Object object) throws IOException { // writeValue具有和writeObject相同的功能 mapper.writeValue( new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aa.txt"),object ); mapper.writeValue(System.out,object ); } /** * * <code>writeMap2Json</code> * @description: TODO(map對(duì)象轉(zhuǎn)換成json對(duì)象) * @param object * @throws IOException * @since 2011-11-8 廖益平 */ public static void writeMap2Json(Object object) throws IOException { System.out.println("使用ObjectMapper-----------"); // writeValue具有和writeObject相同的功能 System.out.println("==>"+mapper.writeValueAsString(object)); mapper.writeValue( new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aamap.txt"),object ); mapper.writeValue( System.out , object ); } /** * * <code>writeList2Json</code> * @description: TODO(list轉(zhuǎn)換成json) * @param object * @throws IOException */ public static void writeList2Json(Object object) throws IOException { System.out.println("==>"+mapper.writeValueAsString(object)); mapper.writeValue( new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aamap.txt"),object ); mapper.writeValue( System.out , object ); } /** * * <code>writeJson2Entity</code> * @description: TODO(json轉(zhuǎn)換成實(shí)體) * @throws IOException */ public static void writeJson2Entity() throws IOException { System.out.println("json串轉(zhuǎn)換成entity-------------"); // File file = new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aa.txt"); // FileInputStream inputStream = new FileInputStream(file); // Student student = mapper.readValue(inputStream,Student.class); // System.out.println(student.toString()); //漂亮輸出 //mapper.defaultPrettyPrintingWriter().writeValueAsString(value); String json = "{\"uid\":1000,\"uname\":\"xiao liao\",\"upwd\":\"123\",\"number\":12.0,\"isstudent\":true}"; Student student1 = mapper.readValue(json,Student.class); System.out.println("json2:"+student1.toString()); } /** * * <code>writeJson2List</code> * @description: TODO(json專(zhuān)程list對(duì)象) * @throws IOException */ public static void writeJson2List() throws IOException { System.out.println("json串轉(zhuǎn)換成entity-------------"); File file = new File("D:\\developSoft\\aaadowload\\testjson1\\lib\\aa.txt"); FileInputStream inputStream = new FileInputStream(file); Student student = mapper.readValue(inputStream,Student.class); System.out.println(student.toString()); String json = "[{\"uid\":1000,\"uname\":\"xiao liao\",\"upwd\":\"123\",\"number\":12.0,\"isstudent\":true},{\"uid\":200,\"uname\":\"xiao mi\",\"upwd\":null,\"number\":0.0,\"isstudent\":false}]"; List<LinkedHashMap<String, Object>> s= mapper.readValue(json,List.class); for (int i = 0; i < s.size(); i++) { LinkedHashMap<String, Object> link = s.get(i); Set<String> key = link.keySet(); for (Iterator iterator = key.iterator(); iterator.hasNext();) { String string = (String) iterator.next(); System.out.println(string+"==>"+link.get(string)); } System.out.println("json:"+i+""+s.get(i).toString()); } } /** * JSON轉(zhuǎn)換為L(zhǎng)ist對(duì)象 */ public static void readJson2List() { String json = "[{\"uid\":1,\"uname\":\"www\",\"number\":234,\"upwd\":\"456\"}," + "{\"uid\":5,\"uname\":\"tom\",\"number\":3.44,\"upwd\":\"123\"}]"; try { List<LinkedHashMap<String, Object>> list = mapper.readValue( json, List.class); System.out.println(list.size()); for (int i = 0; i < list.size(); i++) { Map<String, Object> map = list.get(i); Set<String> set = map.keySet(); for (Iterator<String> it = set.iterator(); it.hasNext();) { String key = it.next(); System.out.println(key + ":" + map.get(key)); } } } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * JSON轉(zhuǎn)換為L(zhǎng)ist對(duì)象 */ public static void readJson2Array() { String json = "[{\"uid\":1,\"uname\":\"www\",\"number\":234,\"upwd\":\"456\"}," + "{\"uid\":5,\"uname\":\"tom\",\"number\":3.44,\"upwd\":\"123\"}]"; try { Student[] students = mapper.readValue(json, Student[].class); for (Student student : students) { System.out.println(">"+student.toString()); } } catch (JsonParseException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
打印結(jié)果 :
串轉(zhuǎn)換成entity------------- json2:uid=1000,name=xiao liao,upwd=123,number=12.0,isStudent=true writeMap2Json ----------- {"2":{"uid":1000,"uname":"xiao liao","upwd":"123","number":12.0,"isstudent":true},"1":{"uid":1000,"uname":"xiao liao","upwd":"123","number":12.0,"isstudent":true}} readJson2Array------------------ >uid=1,name=www,upwd=456,number=234.0,isStudent=false >uid=5,name=tom,upwd=123,number=3.44,isStudent=false writeMap2Json ----------- {"2":{"uid":1000,"uname":"xiao liao","upwd":"123","number":12.0,"isstudent":true},"1":{"uid":1000,"uname":"xiao liao","upwd":"123","number":12.0,"isstudent":true}}
大家逐個(gè)自己試試吧 ,上面也是我的測(cè)試代碼
相關(guān)文章
Java實(shí)現(xiàn)簡(jiǎn)單LRU緩存機(jī)制的方法
這篇文章主要介紹了Java實(shí)現(xiàn)簡(jiǎn)單LRU緩存機(jī)制的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05Struts2學(xué)習(xí)教程之輸入校驗(yàn)示例詳解
這篇文章主要給大家介紹了關(guān)于Struts2學(xué)習(xí)教程之輸入校驗(yàn)的相關(guān)資料,文中通過(guò)示例介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用struts2具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05SpringCloud微服務(wù)熔斷器Hystrix使用詳解
這篇文章主要介紹了Spring Cloud Hyxtrix的基本使用,它是Spring Cloud中集成的一個(gè)組件,在整個(gè)生態(tài)中主要為我們提供服務(wù)隔離,服務(wù)熔斷,服務(wù)降級(jí)功能,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07Java面向?qū)ο蠡A(chǔ)知識(shí)之封裝,繼承,多態(tài)和抽象
這篇文章主要介紹了Java面向?qū)ο蟮姆庋b,繼承,多態(tài)和抽象,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有很好的幫助,需要的朋友可以參考下2021-11-11Java使用GZIP壓縮導(dǎo)致HTTP請(qǐng)求返回亂碼問(wèn)題解決
這篇文章主要為大家介紹了Java壓縮GZIP導(dǎo)致HTTP請(qǐng)求返回亂碼問(wèn)題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06Spring Cloud項(xiàng)目前后端分離跨域的操作
這篇文章主要介紹了Spring Cloud項(xiàng)目前后端分離跨域的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06詳解Nacos中注冊(cè)中心和配置中心的實(shí)現(xiàn)
Spring?Cloud?Alibaba?是阿里巴巴提供的一站式微服務(wù)開(kāi)發(fā)解決方案。而?Nacos?作為?Spring?Cloud?Alibaba?的核心組件之一,提供了兩個(gè)非常重要的功能:注冊(cè)中心和配置中心,我們今天來(lái)了解和實(shí)現(xiàn)一下二者2022-08-08SpringBoot中整合MyBatis-Plus的方法示例
這篇文章主要介紹了SpringBoot中整合MyBatis-Plus的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09