简体   繁体   English

json-简单的containerFactory

[英]json-simple containerFactory

Using the decoding example with the containerFactory, I get the following output: 通过使用containerFactory的解码示例,我得到以下输出:

[{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. 这些是调用entry.getValue()时输出的条目。

How do I get the data out from these objects? 如何从这些对象中获取数据?

For example, how would I get the value of DeviceName. 例如,如何获取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. 尝试使用System.out.println(entry.getValue().getClass())找出值是什么类(或在调试器中检查它)以及实际类的值。 From the output it looks like List<Map<String, String>> or something. 从输出中,它看起来像List<Map<String, String>>或类似的东西。

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

        }

}

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

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