user2823910
user2823910

Reputation: 35

JSON Parser Read Array

Hello i am trying to parse a JSONFILE looking like this :

"ansible_facts": {
    "ansible_all_ipv4_addresses": [
        "xxx"
    ], 
    "ansible_all_ipv6_addresses": [], 
    "ansible_architecture": "xxx", 
    "ansible_bios_date": "xxx", 
    "ansible_bios_version": "xxx", 
    "ansible_cmdline": {
        "KEYBOARDTYPE": "xx", 
        "KEYTABLE": "xx", 
        "LANG": "xxx", 
        "SYSFONT": "xxx", 
        "crashkernel": "xxx", 
        "elevator": "xxx", 
        "nmi_watchdog": "1", 
        "quiet0": true, 
        "rd_LVM_LV": "xxx", 
        "rd_NO_DM": true, 
        "rd_NO_LUKS": true, 
        "rd_NO_MD": true, 
        "rhgb": true, 
        "ro": true, 
        "root": "xxx", 
        "selinux": "0"

...

Here is my Java program :

public static void main(String[] args) {

    JSONParser parser = new JSONParser();

    try {

        String filePath = "C:\\xxx\\xxx\\xxx\\JSON\\file";

        String file = readFileAsString(filePath);

        JSONObject jsonObject = (JSONObject)parser.parse(file);

        JSONArray ansibleFacts = (JSONArray)jsonObject.get("ansible_facts");

        for (Object o : ansibleFacts)
        {
            JSONObject fact = (JSONObject) o;

            String os = (String) fact.get("ansible_architecture");
            System.out.println(os);

            String name = (String) fact.get("ansible_processor_count");
            System.out.println(name);

            String pythonVersion = (String) fact.get("ansible_python_version");
            System.out.println(pythonVersion);
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }

}

private static String readFileAsString(String filePath) throws java.io.IOException{
    byte[] buffer = new byte[(int) new File(filePath).length()];
    BufferedInputStream f = null;
    try {
        f = new BufferedInputStream(new FileInputStream(filePath));
        f.read(buffer);
    } finally {
        if (f != null) try { f.close(); } catch (IOException ignored) { }
    }
    return new String(buffer);
}

I get the error : Exception in thread "main" java.lang.ClassCastException: org.json.simple.JSONObject cannot be cast to org.json.simple.JSONArray.

My JSON file starts directly with an ARRAY, i read several posts but can't understand why it doesn't work...

Upvotes: 1

Views: 1867

Answers (1)

Timuçin
Timuçin

Reputation: 4673

ansible_facts is not an array whereas ansible_all_ipv4_addresses or ansible_all_ipv6_addresses are arrays.

Some examples for accessing specific elements:

JSONObject ansibleFacts = jsonObject.getJSONObject("ansible_facts");
String architecture = ansibleFacts.getString("ansible_architecture");
JSONArray ipv4Addresses = ansibleFacts.getJSONArray("ansible_all_ipv4_addresses");
String xxx = ipv4Addresses.getString(0);

Upvotes: 1

Related Questions