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);
}
运行结果