[Java] Check Json body.

Ref: stackoverflow:How to check whether a given string is valid JSON in Java

public boolean isJSONValid(String test) {
  try {
      new JSONObject(test);
  } catch (JSONException ex) {
      // edited, to include @Arthur's comment
      // e.g. in case JSONArray is valid as well...
      try {
          new JSONArray(test);
      } catch (JSONException ex1) {
          return false;
      }
  }
  return true;
}
This entry was posted in Java. Bookmark the permalink.