用FastJson如何保存Map
package com.iit.json; import com.iit.utils.JsonUtils; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /* 问题:如何序列经一个Map? 思考: 1、Map是双列集合,所以不能直接序列化。 2、将Map放到一个对象中,比如Fox类。 */ public class Demo52 { public static void main(String[] args) { Fox fox = new Fox(); fox.setName("fox1"); Map map = new HashMap(); map.put("1","aa"); map.put("2","ab"); fox.setMap(map); String s1 = JsonUtils.getJsonByObj(fox,true); //System.out.println(s1); List<Fox> list = new ArrayList<>(); list.add(fox); //list.add(fox); Fox fox2 = new Fox(); fox2.setName("fox1"); Map map2 = new HashMap(); map2.put("11","aac"); map2.put("22","abc"); fox2.setMap(map2); list.add(fox2); String s2 = JsonUtils.getJsonByObj(list,true); System.out.println(s2); } } class Fox{ private String name; private Map<String,String> map = new HashMap<>(); public String getName() { return name; } public void setName(String name) { this.name = name; } public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } }
本文来自博客园,作者:xiaoyongdata(微信号:xiaoyongdata),转载请注明原文链接:https://www.cnblogs.com/xiaoyongdata/p/16379068.html