javajsonjson-simple

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 null, why? My JSON does have a keys corresponding to the json.get("key");.

I followed a tutorial and my tree is similar to the tutorial's code. I have printed one of them just to debug, when printing the array values, it just prints out null for every json.get("key"); statment

My 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" 
    }
}

Solution

  • JSONObject json = (JSONObject) obj;
    json = (JSONObject) json.get("release");
    String version = (String) json.get("version");
    

    You can try this. You have to traverse from JSON object root