0

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)

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?


bentaisan
  • 1,056
  • 3
  • 12
  • 29

2 Answers2

3

The problem is that myDevicesInfo is an array of json objects and not a json object. so the following line:

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

needs to change to

JSONArray deviceAttributes = (JSONArray) jsonObject.get("myDevicesInfo");
271828183
  • 599
  • 4
  • 7
  • That helped out with the error, but now, how do I get to the key value pairs? – bentaisan Feb 13 '12 at 00:26
  • JSONArray is java List so you can iterate over it the same way you'd iterate over a List. – 271828183 Feb 13 '12 at 02:30
  • This is the output I get: myDevicesInfo=[{DeviceType=bac, DeviceName=bac, IPAddress=192.168.1.1, UserName=admins, Password=pw}]. I want to get the values in the square brackets. – bentaisan Feb 13 '12 at 17:06
2

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");                    
}
Jeevan Patil
  • 6,029
  • 3
  • 33
  • 50
Sumita
  • 21
  • 3