map创建JSONObject对象

时间:2023-03-08 15:51:33
     public static void mapToJSONObject(){
Map<String, Object> map = new HashMap<String,Object>();
map.put("id", 1);
map.put("name", "杨文培");
map.put("sex", null);
map.put("age", 23);
map.put("birthday", "1993-01-23");
List<String> list = new ArrayList<String>();
list.add("篮球");
list.add("乒乓球");
list.add("羽毛球");
map.put("hobby", list);
map.put("boo", false);
JSONObject jsonObject = JSONObject.fromObject(map);
System.out.println("map.toString()="+map.toString());
System.out.println("jsonObject.toString()="+jsonObject.toString());
try{
Student student = (Student) JSONObject.toBean(jsonObject,Student.class);
System.out.println("student="+student);
}catch(ClassCastException exception){
System.err.println("exception="+exception.getMessage());
}
}