<p>Is there an easy way to return data to web service clients in JSON using java?  I'm fine with servlets, spring, etc.</p>

[{'answer_id': 57732, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p><a href="http://www.json.org/java/index.html" rel="nofollow noreferrer">http://www.json.org/java/index.html</a> has what you need.</p>\n'}, {'answer_id': 57737, 'author': 'Stu Thompson', 'author_id': 2961, 'author_profile': 'https://Stackoverflow.com/users/2961', 'pm_score': 2, 'selected': False, 'text': '<p>Yup!  Check out <a href="http://json-lib.sourceforge.net/" rel="nofollow noreferrer">json-lib</a></p>\n\n<p>Here is a <em>simplified code snippet from my own code</em> that send a set of my domain objects:</p>\n\n<pre><code>private String getJsonDocumenent(Object myObj) (\n    String result = "oops";\n    try {\n        JSONArray jsonArray = JSONArray.fromObject(myObj);\n\n        result = jsonArray.toString(2);  //indent = 2\n\n    } catch (net.sf.json.JSONException je) {\n\n        throw je;\n    }\n    return result;\n}\n</code></pre>\n'}, {'answer_id': 58047, 'author': 'Geekygecko', 'author_id': 6009, 'author_profile': 'https://Stackoverflow.com/users/6009', 'pm_score': 3, 'selected': False, 'text': '<p>We have been using Flexjson for converting Java objects to JSON and have found it very easy to use.\n<a href="http://flexjson.sourceforge.net/" rel="nofollow noreferrer">http://flexjson.sourceforge.net</a></p>\n\n<p>Here are some examples:</p>\n\n<pre><code>public String searchCars() {\n  List&lt;Car&gt; cars = carsService.getCars(manufacturerId);\n  return new JSONSerializer().serialize(cars);\n}\n</code></pre>\n\n<p>It has some cool features such as deepSerialize to send the entire graph and it doesn\'t break with bi directional relationships.</p>\n\n<pre><code>new JSONSerializer().deepSerialize(user); \n</code></pre>\n\n<p>Formatting dates on the server side is often handy too</p>\n\n<pre><code>new JSONSerializer().transform(\n  new DateTransformer("dd/MM/yyyy"),"startDate","endDate"\n).serialize(contract);\n</code></pre>\n'}, {'answer_id': 58124, 'author': 'marcospereira', 'author_id': 4600, 'author_profile': 'https://Stackoverflow.com/users/4600', 'pm_score': 4, 'selected': True, 'text': '<p>To me, <a href="http://xstream.codehaus.org/json-tutorial.html" rel="noreferrer">the best Java &lt;-> JSON parser is XStream</a> (yes, I\'m really talking about json, not about xml). XStream already deals with circular dependencies and has a simple and powerful api where you could write yours drivers, converters and so on.</p>\n\n<p>Kind Regards</p>\n'}, {'answer_id': 62821, 'author': 'Henning', 'author_id': 7034, 'author_profile': 'https://Stackoverflow.com/users/7034', 'pm_score': 1, 'selected': False, 'text': '<p>For RESTful web services in Java, also check out the <a href="http://www.restlet.org/" rel="nofollow noreferrer">Restlet API</a> which provides a very powerful and flexible abstraction for REST web services (both server and client, in a container or standalone), and also integrates nicely with Spring and JSON.</p>\n'}, {'answer_id': 73959, 'author': 'blahspam', 'author_id': 8290, 'author_profile': 'https://Stackoverflow.com/users/8290', 'pm_score': 4, 'selected': False, 'text': '<p>It might be worth looking into <a href="http://jersey.java.net/" rel="noreferrer">Jersey</a>. Jersey makes it easy to expose restful web services as xml and/or JSON. </p>\n\n<p>An example... start with a simple class</p>\n\n<pre><code>@XmlType(name = "", propOrder = { "id", "text" })\n@XmlRootElement(name = "blah")\npublic class Blah implements Serializable {\n    private Integer id;\n    private String text;\n\n    public Blah(Integer id, String text) {\n        this.id = id;\n        this.text = text;\n    }    \n\n    @XmlElement\n    public Integer getId() { return id; }\n    public void setId(Integer id) { this.id = id; }\n\n    @XmlElement\n    public String getText() { return text; }\n    public void setText(String value) { this.text = value; }\n}\n</code></pre>\n\n<p>Then create a Resource </p>\n\n<pre><code>@Path("/blah")\npublic class BlahResource {\n    private Set&lt;Blah&gt; blahs = new HashSet&lt;Blah&gt;();\n\n    @Context\n    private UriInfo context;\n\n    public BlahResource() {\n        blahs.add(new Blah(1, "blah the first"));\n        blahs.add(new Blah(2, "blah the second"));\n    }\n\n    @GET\n    @Path("/{id}")\n    @ProduceMime({"application/json", "application/xml"})\n    public Blah getBlah(@PathParam("id") Integer id) {\n        for (Blah blah : blahs) {\n            if (blah.getId().equals(id)) {\n                return blah;\n            }\n        }\n        throw new NotFoundException("not found");\n    }\n}\n</code></pre>\n\n<p>and expose it. There are many ways to do this, such as by using Jersey\'s ServletContainer. (web.xml)</p>\n\n<pre><code>&lt;servlet&gt;\n    &lt;servlet-name&gt;jersey&lt;/servlet-name&gt;\n    &lt;servlet-class&gt;com.sun.jersey.spi.container.servlet.ServletContainer&lt;/servlet-class&gt;\n    &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;\n&lt;/servlet&gt;\n\n&lt;servlet-mapping&gt;\n    &lt;servlet-name&gt;jersey&lt;/servlet-name&gt;\n    &lt;url-pattern&gt;/*&lt;/url-pattern&gt;\n&lt;/servlet-mapping&gt;\n</code></pre>\n\n<p>Thats all you need to do... pop open your browser and browse to <a href="http://localhost/blah/1" rel="noreferrer">http://localhost/blah/1</a>. By default you will see XML output. If you are using FireFox, install TamperData and change your <code>accept</code> header to <code>application/json</code> to see the JSON output.</p>\n\n<p>Obviously there is much more to it, but <a href="http://jersey.java.net/" rel="noreferrer">Jersey</a> makes all that stuff quite easy.</p>\n\n<p>Good luck!</p>\n'}, {'answer_id': 787025, 'author': 'StaxMan', 'author_id': 59501, 'author_profile': 'https://Stackoverflow.com/users/59501', 'pm_score': 1, 'selected': False, 'text': '<p>As already mentioned, <a href="http://jersey.dev.java.net" rel="nofollow noreferrer">Jersey</a> (JAX-RS impl) is the framework to use; but for basic mapping of Java objects to/from JSON, <a href="http://jackson.codehaus.org/Tutorial" rel="nofollow noreferrer">Tutorial</a> is good. Unlike many alternatives, it does not use strange XML-compatibility conventions but reads and writes clean JSON that directly maps to and from objects. It also has no problems with null (there is difference between missing entry and one having null), empty Lists or Strings (both are distinct from nulls).</p>\n\n<p>Jackson works nicely with Jersey as well, either using JAX-RS provider jar, or even just manually.\nSimilarly it\'s trivially easy to use with plain old servlets; just get input/output stream, call ObjectMapper.readValue() and .writeValue(), and  that\'s about it.</p>\n'}, {'answer_id': 2365261, 'author': 'jon', 'author_id': 226846, 'author_profile': 'https://Stackoverflow.com/users/226846', 'pm_score': 2, 'selected': False, 'text': '<p>I have found google-gson compelling. It converts to JSON and back. <a href="http://code.google.com/p/google-gson/" rel="nofollow noreferrer">http://code.google.com/p/google-gson/</a> It\'s very flexible and can handle complexities with objects in a straightforward manner. I love its support for generics. </p>\n\n<pre><code>/*\n* we\'re looking for results in the form\n* {"id":123,"name":thename},{"id":456,"name":theOtherName},...\n*\n* TypeToken is Gson--allows us to tell Gson the data we\'re dealing with\n* for easier serialization.\n*/\nType mapType = new TypeToken&lt;List&lt;Map&lt;String, String&gt;&gt;&gt;(){}.getType();\n\nList&lt;Map&lt;String, String&gt;&gt; resultList = new LinkedList&lt;Map&lt;String, String&gt;&gt;();\n\nfor (Map.Entry&lt;String, String&gt; pair : sortedMap.entrySet()) {\n    Map&lt;String, String&gt; idNameMap = new HashMap&lt;String, String&gt;();\n    idNameMap.put("id", pair.getKey());\n    idNameMap.put("name", pair.getValue());\n    resultList.add(idNameMap);\n}\n\nreturn (new Gson()).toJson(resultList, mapType);\n</code></pre>\n'}, {'answer_id': 3757053, 'author': 'Jackie', 'author_id': 410289, 'author_profile': 'https://Stackoverflow.com/users/410289', 'pm_score': 1, 'selected': False, 'text': '<p>I have been using jaxws-json, for providing JSON format web services. you can check the project <a href="https://jax-ws-commons.dev.java.net/json/" rel="nofollow noreferrer">https://jax-ws-commons.dev.java.net/json/</a>.</p>\n\n<p>it\'s a nice project, once you get it up, you\'ll find out how charming it is.</p>\n'}]
['https://Stackoverflow.com/questions/57689', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5988/']