Json.simple returns null 当我尝试获取我的 JSON 值时,即使我的 JSON 中有一个值
Json.simple returns null when I try to get my JSON Values even if I have a value in my JSON
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 returns 为空,为什么?我的 JSON 确实有一个键对应于 json.get("key");
.
我遵循了一个教程,我的树与教程的代码相似。我打印了其中一个只是为了调试,当打印数组值时,它只是为每个 json.get("key");
statment
打印出 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"
}
}
JSONObject json = (JSONObject) obj;
json = (JSONObject) json.get("release");
String version = (String) json.get("version");
你可以试试这个。
你必须从 JSON object root
遍历
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 returns 为空,为什么?我的 JSON 确实有一个键对应于 json.get("key");
.
我遵循了一个教程,我的树与教程的代码相似。我打印了其中一个只是为了调试,当打印数组值时,它只是为每个 json.get("key");
statment
我的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"
}
}
JSONObject json = (JSONObject) obj;
json = (JSONObject) json.get("release");
String version = (String) json.get("version");
你可以试试这个。 你必须从 JSON object root
遍历