HashMap转JSON字符串,JSON字符串转HashMap
程序员文章站
2024-01-30 16:35:22
...
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));
System.out.println("json格式:" + jsonObject.getString("test"));
String test = jsonObject.getString("jsonTest");
HashMap<Long,String> hashMap1 = JSON.parseObject(test, HashMap.class);
for (Long key : hashMap1.keySet()) {
String value = hashMap1.get(key);
System.out.println(key + ":" + value);
}
运行结果