Issue
I am creating a JSON warning using:
String warningString = "{\"empty\":\"No YM specific lines\"}";
json = new Gson().toJson(warningString);
json = json.replaceAll("^['\"]*", "").replaceAll("['\"]*$", "").replace("\\", "");
While this works I though there would be a better way to do this and found:
import com.google.gwt.thirdparty.json.JSONObject;
JSONObject obj = new JSONObject();
try {
obj.put("empty", "No YM specific lines");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
However, this results in a status 404 when I test. I have checked the logs and can not find any errors. I am using Apache Tomcat/9.0.46.
Changed to:
import org.json.JSONException;
import org.json.JSONObject;
String json = null;
if (ymSpecificLineDetail == null) {
//Gracefully return a warning message
json = new JSONObject().put("empty", "No YM specific lines").toString();
} else {
json = new Gson().toJson(ymSpecificLineDetail);
}
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
Solution
Change your imports to:
import org.json.JSONException;
import org.json.JSONObject;
Answered By - lkatiforis