簡體   English   中英

json-簡單的containerFactory

[英]json-simple containerFactory

通過使用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);
          }

嘗試使用System.out.println(entry.getValue().getClass())找出值是什么類(或在調試器中檢查它)以及實際類的值。 從輸出中,它看起來像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