欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何用ObjectMapper將復(fù)雜Map轉(zhuǎn)換為實(shí)體類

 更新時(shí)間:2021年08月19日 09:30:16   作者:墨一般的白色  
這篇文章主要介紹了如何用ObjectMapper將復(fù)雜Map轉(zhuǎn)換為實(shí)體類的操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

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)換

ObjectMapper mapper = new ObjectMapper();
Resource resource = mapper.convertValue(Map, Resource.class);

注意的點(diǎn):實(shí)體類中的字段是駝峰的 首字母不能大寫。

objectMapper 解析復(fù)雜json toMap

public void parseSettings(){
        
        String settings = "{\"基本設(shè)置\":{\"name\":\"基本設(shè)置\",\"entries\":[{\"name\":\"站號\",\"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\":\"父井號\",\"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\"}]}}";
        ObjectMapper objectMapper = new ObjectMapper();
        JavaType javaType = objectMapper.getTypeFactory().constructMapType(HashMap.class, String.class, SettingGroup.class);
        try {
            Map<String,SettingGroup>    mapSettings =    objectMapper.readValue(settings, javaType);
             SettingGroup group = mapSettings.get("基本設(shè)置");
             for (SettingEntry entry : group.getEntries()) {
                 System.out.println(entry.getName());                    
             }
            
             group = mapSettings.get("油閥間");
             for (SettingEntry entry : group.getEntries()) {
                 System.out.println("entry.Name:"+entry.getName()+" entry.getValue:"+entry.getValue());
             }
        } catch (JsonParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }        
    }

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論