1
我試圖用Java獲取http://status.mojang.com/check中列出的信息。 我知道這是JSON,所以我試圖在互聯網上使用varoius示例獲取該信息,但我似乎無法弄清楚什麼是錯的。Java - 獲取JSON對象數組?
這裏是我的代碼: http://pastebin.com/USYy93kZ
有誰知道我能做到這一點?
我試圖用Java獲取http://status.mojang.com/check中列出的信息。 我知道這是JSON,所以我試圖在互聯網上使用varoius示例獲取該信息,但我似乎無法弄清楚什麼是錯的。Java - 獲取JSON對象數組?
這裏是我的代碼: http://pastebin.com/USYy93kZ
有誰知道我能做到這一點?
-嘗試在你的類中使用下面的方法來解析JSON。
private static String readAll(BufferedReader rd) throws IOException {
StringBuilder sb = new StringBuilder();
String cp = new String();
while((cp=rd.readLine())!=null){
sb.append(cp);
}
return sb.toString();
}
public static JSONArray readJsonFromUrl(String url) throws IOException, JSONException {
InputStream is = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONArray arr = new JSONArray(jsonText);
//System.out.println(arr.toString(2));
return arr;
} finally {
is.close();
}
}
public JSONArray go() throws IOException, JSONException {
JSONArray json = readJsonFromUrl("http://www.comparestructuredproducts.com/AppData.aspx");
return json;
}