public static void smth()
throws ParseException, FileNotFoundException, IOException, InterruptedException {
JSONParser parser = new JSONParser();
FileReader reader = new FileReader("release.json");
Object obj = parser.parse(reader); // Parse JSON data of file
JSONObject json = (JSONObject) obj;
String version = (String) json.get("version");
String license = (String) json.get("license");
String licenseFile = (String) json.get("LICENSE.txt?");
String date = (String) json.get("date");
String author = (String) json.get("author");
String contrib = (String) json.get("contributors");
String lib = (String) json.get("libraries");
String[] values = { version, license, author, contrib, date, lib, licenseFile };
for (int i = 0; i < values.length; i ) {
System.out.println(values[i]);
}
}
JSON.simple 回傳 null,為什么?我的 JSON 確實有一個與json.get("key");.
我遵循了一個教程,我的樹類似于教程的代碼。我列印了其中一個只是為了除錯,在列印陣列值時,它只是為每個json.get("key");陳述句列印出 null
我的 JSON:
{
"release": {
"version": "0.0.1 InDev",
"license": "GNU General Public License v3",
"LICENSE.txt?" : "Webber/LICENSE.txt",
"date" : "18th Februaru, 2022 @ 4:59PM Indian Standard Time",
"author" : "Habis Muhammed",
"contributors" : "***** ?? Nobody *****",
"libraries":"json-simple"
}
}
uj5u.com熱心網友回復:
JSONObject json = (JSONObject) obj;
JSONObject releaseJson = json.getJSONObject("release");
String version = (String) releaseJson.get("version");
你可以試試這個。您必須從 JSON 物件根遍歷
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/429726.html
