Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
    "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;
JSONObject releaseJson = json.getJSONObject("release");
String version = (String) releaseJson.get("version");

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading