使用json.org.jar进行数据的解析
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
String qq = input.next();
//使用json.org.jar包进行json格式的解析
String path = "http://japi.juhe.cn/qqevaluate/qq?qq="+qq+"&key=3c7df0dc92d57c730516fc3722a67fea";
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.connect();
InputStream ins = conn.getInputStream();
int len = -1;
byte[]bytes = new byte[512];
StringBuffer sb = new StringBuffer();
while((len=ins.read(bytes))!=-1){
sb.append(new String(bytes,0,len));
}
ins.close();
System.out.println("over");
//System.out.println(sb.toString());
//1. 创建JsonObject对象 new JsonObject("jsonStr");
JSONObject jsonObject = new JSONObject(sb.toString());
String result = jsonObject.getString("reason");
System.out.println(result);
/*
通过key 1.直接获取数据:
2.获取json对象
3.获取json对象
* */
jsonObject = jsonObject.getJSONObject("result");
jsonObject = jsonObject.getJSONObject("data");
System.out.println(jsonObject.getString("conclusion"));
JSONArray arr = jsonObject.getJSONArray("");
for (int i = 0; i < arr.length(); i++) {
}
}