Ankur Ankur - 1 year ago 122
JSON Question

How do you return a JSON object from a Java Servlet

How do you return a JSON object form a Java servlet.

Previously when doing AJAX with a servlet I have returned a string. Is there a JSON object type that needs to be used, or do you just return a String that looks like a JSON object e.g.

String objectToReturn = "{ key1: 'value1', key2: 'value2' }";

Answer Source

I do exactly what you suggest (return a String).

You might consider setting the MIME type to indicate you're returning JSON, though (according to this other stackoverflow post it's "application/json").

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download