如何在Java中验证字符串是否为JSON?
问题描述:
I've already said, my question is different...question is already asked here, but i want a predefined method in java which checksgiven string is json format or not.
如果没有预定义的方法,则至少告诉一个代码是否检查JSON格式,而不使用try catch块.
if there is no predefined method then at least tell a code which checks JSON Format or not, without using try catch block..
预先感谢...
答
使用String中的 JSONObject的构造函数
Use JSONObject's constructor from a String
try {
JSONObject o = new JSONObject(yourString);
} catch (JSONException e) {
LOGGER.error("No valid json");
}