怎么用ObjectMapper将Map转换为实体类
生活随笔
收集整理的這篇文章主要介紹了
怎么用ObjectMapper将Map转换为实体类
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
本篇內(nèi)容主要講解“怎么用ObjectMapper將Map轉(zhuǎn)換為實(shí)體類”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“怎么用ObjectMapper將Map轉(zhuǎn)換為實(shí)體類”吧!
目錄
-
ObjectMapper將復(fù)雜Map轉(zhuǎn)為實(shí)體類
-
背景
-
1.使用ObjectMapper要添加的依賴是
-
2.將復(fù)雜Map開始轉(zhuǎn)換
-
objectMapper 解析復(fù)雜json toMap
ObjectMapper將復(fù)雜Map轉(zhuǎn)為實(shí)體類
背景
用fastjson轉(zhuǎn)換復(fù)雜Map是淺層的可以轉(zhuǎn) 深層的轉(zhuǎn)換為null.,這時(shí)候可以用jackson來轉(zhuǎn)。
1.使用ObjectMapper要添加的依賴是
<!--添加jackson包--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.3</version> </dependency>
2.將復(fù)雜Map開始轉(zhuǎn)換
ObjectMappermapper=newObjectMapper(); Resourceresource=mapper.convertValue(Map,Resource.class);
注意的點(diǎn):實(shí)體類中的字段是駝峰的 首字母不能大寫。
objectMapper 解析復(fù)雜json toMap
publicvoidparseSettings(){
Stringsettings="{\"基本設(shè)置\":{\"name\":\"基本設(shè)置\",\"entries\":[{\"name\":\"站號(hào)\",\"type\":\"number\",\"description\":\"\",\"value\":\"1\"},{\"name\":\"AppEUI\",\"type\":\"string\",\"description\":\"\",\"value\":\"2C26C503A0010006\"},{\"name\":\"DevEUI\",\"type\":\"string\",\"description\":\"\",\"value\":\"4A7703A000FC09\"},{\"name\":\"命令次數(shù)\",\"type\":\"number\",\"description\":\"\",\"value\":\"2\"},{\"name\":\"父井號(hào)\",\"type\":\"string\",\"description\":\"\",\"value\":\"test11\"}]},\"油閥間\":{\"name\":\"油閥間\",\"entries\":[{\"name\":\"上傳間隔時(shí)間\",\"type\":\"number\",\"description\":\"\",\"value\":\"2\"},{\"name\":\"設(shè)定值\",\"type\":\"number\",\"description\":\"\",\"value\":\"10\"},{\"name\":\"波動(dòng)值\",\"type\":\"number\",\"description\":\"\",\"value\":\"1\"},{\"name\":\"控制模式\",\"type\":\"number\",\"description\":\"\",\"value\":\"0\"},{\"name\":\"調(diào)節(jié)上限\",\"type\":\"number\",\"description\":\"\",\"value\":\"15\"},{\"name\":\"調(diào)節(jié)下限\",\"type\":\"number\",\"description\":\"\",\"value\":\"5\"},{\"name\":\"調(diào)節(jié)時(shí)間\",\"type\":\"number\",\"description\":\"\",\"value\":\"10\"}]},\"采集調(diào)度\":{\"name\":\"采集調(diào)度\",\"entries\":[{\"name\":\"interval\",\"type\":\"number\",\"description\":\"\",\"value\":\"2\"}]}}";
ObjectMapperobjectMapper=newObjectMapper();
JavaTypejavaType=objectMapper.getTypeFactory().constructMapType(HashMap.class,String.class,SettingGroup.class);
try{
Map<String,SettingGroup>mapSettings=objectMapper.readValue(settings,javaType);
SettingGroupgroup=mapSettings.get("基本設(shè)置");
for(SettingEntryentry:group.getEntries()){
System.out.println(entry.getName());
}
group=mapSettings.get("油閥間");
for(SettingEntryentry:group.getEntries()){
System.out.println("entry.Name:"+entry.getName()+"entry.getValue:"+entry.getValue());
}
}catch(JsonParseExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(JsonMappingExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
總結(jié)
以上是生活随笔為你收集整理的怎么用ObjectMapper将Map转换为实体类的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python处理xml文件_Python
- 下一篇: ai字体如何加粗