在写topo图的时候,后台给前台传的json字符串数据中含有中文,前台查看发现是乱码
部分代码是这样:
...
data.put("topoLinkList",topoLinkList);
data.put("topoNetDeviceList",topoNetDeviceList);
data.put("topoNodeDeviceList",topoNodeDeviceList);
data.put("height",(distanceOfCenterToNetdevice+distanceOfHubToNode+distanceOfNetdeviceToNode));
data.put("width",(distanceOfCenterToNetdevice+distanceOfHubToNode+distanceOfNetdeviceToNode));
return JSON.toJSONStringWithDateFormat(data,"yyyy-MM-dd HH:mm:ss",SerializerFeature.DisableCircularReferenceDetect);
data:是一个带有中文的集合
转为json字符串传给前台。
这里转换用的是JSON.toJSONStringWithDateFormat(data,“yyyy-MM-dd HH:mm:ss”,SerializerFeature.DisableCircularReferenceDetect);
而不是JSON.toString(data)。
原因是:topoLinkList和topoNetDeviceList集合中含有相同的元素,当你查看数据的时候会发现
只有其中一个数组中有数据,另一个是指明该数据在哪个位置,即保证相同的数据只存在一个地方,其他的都是指明位置
中文乱码问题:
@RequestMapping(value = "getTopoInitInfo.do",produces="text/html;charset=UTF-8")
在请求映射加上这句就可以:produces=“text/html;charset=UTF-8”