问题:
使用net.sf.json的JSONObject.fromObject转json字符串未json对象时出现数字精度丢失问题
public static Map<String, Object> json2MapOld(String jsonString) {
JSONObject jsonObject = JSONObject.fromObject(jsonString);
Iterator keyIter = jsonObject.keys();
String key;
Object value;
Map<String, Object> valueMap = new HashMap<String, Object>();
while (keyIter.hasNext()) {
key = (String) keyIter.next();
value = jsonObject.get(key);
valueMap.put(key, value);
}
return valueMap;
}
改用com.alibaba.fastjson后正常
public static Map<String, Object> json2Map(String jsonString) {
Map<String, Object> valueMap = new HashMap<String, Object>();
com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(jsonString);
String key;
Object value;
for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
key = (String) entry.getKey();
value = entry.getValue();
valueMap.put(key, value);
}
//valueMap = (Map<String,Object>)jsonObject;
return valueMap;
}
相关参考:
字符串转换为JSON对象,数据精度丢失
json-lib反序列化精度丢失问题
com.alibaba.fastjson把JSONObject的一些操作
FastJson-fastjson的简单使用(alibaba)
fastjson对象,JSON,字符串,map之间的互转
com.alibaba.fastjson和net.sf.json用法区别
fastjson与net.sf.json区别
提取多层嵌套JSON类型数据