【MapSheep】
[好记性不如烂笔头]

FastJSON没有提供直接判断一个JSON字符串是JSONObject或JSONArray的方法,因此,对于一个未知的JSON串,我们需要自行判断。

方法一

  1. 实例代码
    String jsonStr = "[{\"aaa\" : \"111\"}]";
    Object object = JSON.parse(jsonStr);

    if (object instanceof JSONObject) {
        JSONObject jsonObject = (JSONObject) object;
        System.out.print("jsonObject:" + jsonObject);
    } else if (object instanceof JSONArray) {
        JSONArray jsonArray = (JSONArray) object;
        System.out.print("jsonArray:" + jsonArray);
    } else {
        System.out.println("Neither jsonobject nor jsonarray is jsonStr");
    }

方法二

  1. 示例代码
private boolean isjson(String str) {
    try {
        JSONObject jsonStr = JSONObject.parseObject(str);
        return  true;
    } catch (Exception e) {
        return false;
    }
}
posted on 2022-07-06 11:11  (Play)  阅读(3100)  评论(0编辑  收藏  举报