简体   繁体   English

JSON解析器读取数组

[英]JSON Parser Read Array

Hello i am trying to parse a JSONFILE looking like this : 您好,我正在尝试解析一个JSONFILE,如下所示:

"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 : 这是我的Java程序:

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. 我收到错误:线程“ main”中的异常java.lang.ClassCastException:org.json.simple.JSONObject无法转换为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... 我的JSON文件直接以ARRAY开头,我读了几篇文章,但不明白为什么它不起作用...

ansible_facts is not an array whereas ansible_all_ipv4_addresses or ansible_all_ipv6_addresses are arrays. ansible_facts不是数组,而ansible_all_ipv4_addressesansible_all_ipv6_addresses是数组。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM