2012-02-14 43 views
0

、私は、次の出力を得る:JSON-簡単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); 
      } 

答えて

1

値はSystem.out.println(entry.getValue().getClass())である(またはデバッガでそれを点検)どのようなクラスを見つけ出す試してみて、実際のクラスへの値。出力からはList<Map<String, String>>か何かのように見えます。

+0

ああ...いいアイデア!出力はjava.lang.Stringクラスです。最初のparser.parse文にキーと値のペアがある方法はありますか? – bentaisan

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

     } 

}