記錄下json與map的轉換

map轉json字符串

public void map2JsonObj() {
        Map<String, Object> map = new HashMap<>();
        map.put("name", "msg");
        map.put("type", "long");
        map.put("comment", "消息");
        System.out.println(JSON.toJSONString(map));
    }

listMap2JsonListObj

List<Map<String, Object>> mapList = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("name", "msg");
        map.put("type", "long");
        map.put("comment", "消息");
        mapList.add(map);
        Map<String, Object> map2 = new HashMap<>();
        map2.put("name", "msg");
        map2.put("type", "long");
        map2.put("comment", "消息");
        mapList.add(map2);
        System.out.println(JSON.toJSONString(mapList));

jsonObj2Map

public void jsonObj2Map() {
        HashMap map = JSON.parseObject("{\"name\":\"msg\",\"comment\":\"消息\",\"type\":\"long\"}", HashMap.class);
        System.out.println(map);
    }

jsonListObj2ListMap

public void jsonListObj2ListMap() {
        ArrayList<Map<String, Object>> list = JSON.parseObject("[{\"name\":\"msg\",\"comment\":\"消息\",\"type\":\"long\"},{\"name\":\"msg\",\"comment\":\"消息\",\"type\":\"long\"}]", ArrayList.class);
        for (Map<String, Object> stringObjectMap : list) {
            System.out.println(stringObjectMap);
        }
    }
相關文章
相關標籤/搜索