json-simple containerFactory

时间:2012-02-14 00:54:52

标签: java json-simple

使用与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}]

这些是调用entry.getValue()时输出的条目。

如何从这些对象中获取数据?

例如,我如何获得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);
          }

2 个答案:

答案 0 :(得分:1)

尝试使用System.out.println(entry.getValue().getClass())(或在调试器中检查它)以及实际类的值来查找值的类。从输出看起来像List<Map<String, String>>或其他东西。

答案 1 :(得分:0)

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

        }

}