1I use Gson class in order to convert a
2Json object into a java object.
3If it works without any error, it means that it is a valid json format...
4
5import com.google.gson.Gson;
6
7public class JSONUtils {
8
9 Gson gson = new Gson();
10
11 public boolean isJSONValid(String jsonInString) {
12 try {
13 gson.fromJson(jsonInString, Object.class);
14 return true;
15 } catch(com.google.gson.JsonSyntaxException e) {
16 return false;
17 }
18 }
19}
1//extensive check to make sure object is not of string type and not null
2function isJson(item) {
3 item = typeof item !== "string"
4 ? JSON.stringify(item)
5 : item;
6
7 try {
8 item = JSON.parse(item);
9 } catch (e) {
10 return false;
11 }
12
13 if (typeof item === "object" && item !== null) {
14 return true;
15 }
16
17 return false;
18}