I would recommend using Gson for this. It has the advantage that it supports generics and nested beans very well and it is also fast enough. I've posted a Gson#fromJson()
example which converts a fairly complex JSON string to Java here: Converting JSON to Java
The Gson#toJson()
to convert any valid Javabean-like object to JSON is basically a piece of cake:
String json = new Gson().toJson(object);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
Edit: Once a Java object is represented in JSON, does this mean that JavaScript can parse it and convert it to the corresponding JavaScript objects?
Sure you can access them like a JS object. If you're new to using JSON in JS as well, then I can recomment this kickoff tutorial: http://www.hunlock.com/blogs/Mastering_JSON_%28_JavaScript_Object_Notation_%29