简体   繁体   中英

json-simple containerFactory

Using the decoding example with the containerFactory, I get the following output:

[{DeviceType=foo, DeviceName=foo1, IPAddress=192.168.1.1, UserName=admin, Password=pw},     {DeviceType=foo, DeviceName=foo2, IPAddress=192.168.1.2, UserName=admin, Password=pw}]

These are the entries that are outputted when entry.getValue() is called.

How do I get the data out from these objects?

For example, how would I get the value of DeviceName.

Map obj = (Map)parser.parse(new FileReader("example.yml"),containerFactory);

Iterator iter = obj.entrySet().iterator();

//Iterator iterInner = new Iterator();

while(iter.hasNext()){
    Map.Entry entry = (Map.Entry)iter.next();
    System.out.println(entry.getValue());                                
            }

            //System.out.println("==toJSONString()==");
            //System.out.println(JSONValue.toJSONString(json));

      } catch (FileNotFoundException e) {
            e.printStackTrace();
      } catch (IOException e) {
            e.printStackTrace();
      } catch(ParseException pe){
            System.out.println(pe);
          }

Try finding out what classes the values are with System.out.println(entry.getValue().getClass()) (or inspect it in debugger) and the values to actual classes. From the output it looks like List<Map<String, String>> or something.

//The innermost data is in a LinkedHashMap
//I hope this snippet helps someone
while(iter.hasNext)
    {
        LinkedList entry = (LinkedList)iter.next();
        LinkedList myList = new LinkedList();
        Map.Entry entry = (Map.Entry)iter.next();
        System.out.println(entry.getValue().getClass());
        myList = (LinkedList) (entry.getValue());
        System.out.println(myList);
        Iterator iterate = myList.iterator();
        while (iterate.hasNext())
        {
            LinkedHashMap entry2 = (LinkedHashMap)iterate.next();
            System.out.println(entry2.get("DeviceName"));

        }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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