1.Fastjson
我们通常在已知格式的情况下直接使用JSONObject,JSONArray,但是如果遇到需要判断格式呢?
try{
Object object=JSON.parse(a);if (object instanceofJSONObject){//JSONObject
}if (object instanceofJSONArray){//JSONArray
}
}catch(com.alibaba.fastjson.JSONException e){//非JSON字符串
}
2.org.json.JSON
直接使用JSON库做解析的情况不多,但是这里也稍微写一下
log.info(JSON.parse(jsonStr).getClass().getName());try{
Object json= newJSONTokener(jsonStr).nextValue();
log.info( json.getClass().toString());//json.toString();
if(json instanceofJSONObject){
log.info("is JSONObject");
JSONObject jsonObject=(JSONObject)json;//further actions on jsonObjects//...
}else if (json instanceofJSONArray){
log.info("is JSONArray");
JSONArray jsonArray=(JSONArray)json;//further actions on jsonArr