一、maven 引入
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
二、代码:
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONTokener;
public static void main(String[] args) {
// String jsonStr = "{\"id\":\"1\",\"name\":\"wangjing\"}"; //json字符串
String jsonStr = "[{\"id\":\"1\",\"name\":\"wangjing\"},{\"id\":\"2\",\"name\":\"wangjingjing\"}]"; //jsonArray字符串
Object json = new JSONTokener(jsonStr).nextValue();
if(json instanceof JSONObject){
System.out.println("这是一个json字符串");
}else if (json instanceof JSONArray){
System.out.println("这是一个JsonArray字符串");
}
}
注:以上内容仅提供参考和交流,请勿用于商业用途,如有侵权联系本人删除