com.alibaba.fastjson过滤问题

在使用alibaba.fastjson将数据转成json的时候,有一种情况会过滤元素,以下以城市和热点城市举例:

1、所有城市信息以一个List保存所有城市元素,每个元素以map表示一个城市相关信息,map以城市名为key,以城市具体信息为value

2、热点城市信息以一个List保存自定义的几个城市,每个元素以map表示一个热点城市相关信息,map以城市名为key,以城市具体信息为value,热点城市map是直接引用所有城市信息List中的元素

假设某个应用或者app后台需要返回城市及热点城市列表及信息给前端,相关城市信息以如下方式保存,其中热点城市的key-value对在直接引用城市信息中现成的:

public static List<Hash<String,Object>> switchCityInfoList = new ArrayList<HashMap<String,Object>>;

public static List<Hash<String,Object>> hotCityList = new ArrayList<HashMap<String,Object>>;

Map<String,Object> ret = new HashMap<String,Object>();

ret.put("retCode","0");

ret.put("hotCity",hotCityList);

ret.put("switchCityInfo",switchCityInfoList);

这里使用fastjson处理ret的时候,ret会过滤掉相同的map子元素,结果就是热点城市json对正常,而城市信息json对就少了热点城市几个城市信息,解决办法是,在添加热点城市及其信息到hotCityList时,不能直接引用switchCityInfoList中已有的数据,要重新new一个出来:

......此处省略部分

HashMap<String,Object> hotCityInfo = switchCityInfoMap.get(cityName)

hotCityList.add(new HashMap<String,Object>(hotCityInfo));


猜你喜欢

转载自blog.csdn.net/zhoutao2/article/details/79523000