使用JSONObject.fromObject(student)可以将java对象转为JSONObject。
/**
* java对象转换成json对象,并获取json对象属性
*/
JSONObject jsonStu = JSONObject.fromObject(student);
System.out.println(jsonStu.toString());
System.out.println(jsonStu.getJSONArray("hobby"));
来源:
net.sf.json.JSONObject实现Object对象与Json字符串的互转_wjxbj的博客-CSDN博客