简体   繁体   English

json-simple,从文件读取

[英]json-simple, reading from a file

I am trying to iterate through a file in the file system which contains configuration information for numerous devices. 我试图遍历文件系统中的一个文件,该文件包含许多设备的配置信息。

The file is in this format: 该文件的格式如下:

 {
    "myDevicesInfo":
    [
        {
            "DeviceType":"foo", 
            "DeviceName":"foo1", 
            "IPAddress":"192.168.1.1", 
            "UserName":"admin", 
            "Password":"pw"
        }
    ]
}

I am getting the following error when trying to get the inner key-value pairs: 尝试获取内部键值对时出现以下错误:

Exception in thread "main" java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject at mav2bac.loadDevices(bac.java:98) at mav2bac.main(bac.java:70) 线程“主”中的异常java.lang.ClassCastException:org.json.simple.JSONArray无法在mav2bac.main(bac.java:98)的mav2bac.loadDevices(bac.java:98)处转换为org.json.simple.JSONObject。 70)

File appBase = new File("."); //current directory
            String path = appBase.getAbsolutePath();
            System.out.println(path);

            Object obj = parser.parse(new FileReader("bac.yml"));

            JSONObject jsonObject = (JSONObject) obj;
            JSONObject jsonObjectDevice = (JSONObject)jsonObject;
            JSONObject deviceAttributes = (JSONObject) jsonObject.get("myDevicesInfo");

            Map json = (Map)parser.parse(jsonObject.toJSONString(), containerFactory);
            System.out.println(json.values());
            Iterator iter = json.entrySet().iterator();
            System.out.println("==iterate result==");
            while(iter.hasNext()){
              Map.Entry entry = (Map.Entry)iter.next();
              //System.out.println(entry.getKey() + "=>" + entry.getValue());
              System.out.println(entry.getValue());
            }

So what is the proper way to get convert use ContainerFactory and instantiate an object containing these values? 那么,使用ContainerFactory进行转换并实例化包含这些值的对象的正确方法是什么?


The problem is that myDevicesInfo is an array of json objects and not a json object. 问题是myDevicesInfo是json对象的数组,而不是json对象。 so the following line: 所以下面这行:

JSONObject deviceAttributes = (JSONObject) jsonObject.get("myDevicesInfo");

needs to change to 需要更改为

JSONArray deviceAttributes = (JSONArray) jsonObject.get("myDevicesInfo");

Try this : 尝试这个 :

JSONParser parser = new JSONParser();
JSONObject jsonObject = (JSONObject) parser.parse(new FileReader(pathToJsonFile));
JSONArray features = (JSONArray) jsonObject.get("myDevicesInfo");
Iterator itr=features.iterator();

while(itr.hasNext()){
    JSONObject featureJsonObj = (JSONObject)itr.next();
    String deviceType = (String)featureJsonObj.get("DeviceType");
    String deviceName = (String) featureJsonObj.get("DeviceName");
    String ipadd = (String) featureJsonObj.get("IPAddress");
    String uname = (String) featureJsonObj.get("UserName");
    String pwd = (String) featureJsonObj.get("Password");                    
}

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

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