When I want to return JSON in my service, with the @Produces(MediaType.APPLICATION_JSON) annotation, it works well for POJO. (I am using Jackson, as explained in the Jersey's documentation).
But if I return a simple String, the result is not really what I expected. The String is returned but with no quotes to encapsulate it; thus, I am not able to parse it in my JavaScript client using JQuery.
Is this the normal behavior? Is there a way to add the quotes to the result?
You can simply construct JSONObject:
JSONObject myObject = new JSONObject();
myObject.JSONObject myObject = new JSONObject();
try {
myObject.put("name", "Agamemnon");
myObject.put("age", 32);
}
catch (JSONException ex) {
LOGGER.log(Level.SEVERE, "Error ...", ex);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With