HashMap<Long,String> hashMap = new HashMap<>();
hashMap.put(1000000000001L,"一");
hashMap.put(1000000000002L,"二");
hashMap.put(1000000000003L,"三");
JSONObject jsonObject = new JSONObject();
jsonObject.put("jsonTest",JSONObject.toJSONString(hashMap)); //HashMap转json字符串
System.out.println("HashMap转json字符串的结果为:");
System.out.println(jsonObject.getString("jsonTest"));
String jsonString = jsonObject.getString("jsonTest");
HashMap<Long,String> hashMap1 = JSON.parseObject(jsonString, HashMap.class); //json字符串转HashMap
System.out.println("json字符串转HashMap,遍历HashMap取key:valued的值为:");
for (Long key : hashMap1.keySet()) {
String value = hashMap1.get(key);
System.out.println(key + ":" + value);
}
运行结果

本文详细介绍了如何使用HashMap将Long和String类型的数据转换为JSON字符串,并演示了如何从JSON字符串逆向转换回HashMap,通过示例展示了关键操作和结果输出。

被折叠的 条评论
为什么被折叠?



