如何在java中从映射添加/检索映射?

如何在java中从映射添加/检索映射?,java,hashmap,Java,Hashmap,我试图编写具有普通键值对以及键值(map)的对象,即具有map作为值的键 myobject={param3={key1=key1-value1,key2=key2-value2},param1=value1,param2=value2} 这里的对象有param1,param2值作为字符串,param3值作为另一个映射 那么,如何正确地将其转换为映射,然后写入文件呢 Object songObj = (Object) executionContext.getBeanContext().getBe

我试图编写具有普通键值对以及键值(map)的对象,即具有map作为值的键

myobject={param3={key1=key1-value1,key2=key2-value2},param1=value1,param2=value2}

这里的对象有param1,param2值作为字符串,param3值作为另一个映射

那么,如何正确地将其转换为映射,然后写入文件呢

 Object songObj = (Object) executionContext.getBeanContext().getBean("myobject");
 // myobject = {param1=value1, param2=value2,param3={key1=key1-value1, key2=key2-value2}}
    HashMap mp = (HashMap) getMapTest1(songObj);
 // HashMap mp = (HashMap) getMapTest2(songObj);
    this.write(mp);

   public static Map<String, Object> getMapTest1(Object obj) {
    ObjectMapper m = new ObjectMapper();
    Map<String, Object> props = m.convertValue(obj, Map.class);
    return props;
   }

  public static Map<String, Object> getMapTest2(Object obj) throws JsonParseException, IOException{
    ObjectMapper mapper = new ObjectMapper();
    JsonFactory factory = new JsonFactory();
    JsonParser json  = factory.createParser(obj.toString());
    TypeReference<Map<String, Object>> typeReference = new TypeReference<Map<String, Object>>() {};
    Map<String, Object> map = mapper.readValue(json, typeReference);        
    System.out.println(map);
    return map;
 }

  public void write(HashMap mp) throws IOException {
    // TODO Auto-generated method stub

    Writer nodeWriter = writer;
    Iterator it = mp.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pair = (Map.Entry) it.next();
        System.out.println(pair.getKey() + " = " + pair.getValue());
        if (pair != null) {
            nodeWriter.write(pair.getValue().toString());
        }
        nodeWriter.write(",");
    }
    writer.flush();
}


  output should be writen in file as...
  ------------------------------------
  value1,value2
  key1-value1,key2-value2
Object songObj=(Object)executionContext.getBeanContext().getBean(“myobject”);
//myobject={param1=value1,param2=value2,param3={key1=key1-value1,key2=key2-value2}}
HashMap mp=(HashMap)getMapTest1(songObj);
//HashMap mp=(HashMap)getMapTest2(songObj);
这个。写(mp);
公共静态映射getMapTest1(对象obj){
ObjectMapper m=新的ObjectMapper();
Map props=m.convertValue(obj,Map.class);
返回道具;
}
公共静态映射getMapTest2(对象obj)抛出JsonParseException、IOException{
ObjectMapper mapper=新的ObjectMapper();
JsonFactory工厂=新的JsonFactory();
JsonParser json=factory.createParser(obj.toString());
TypeReference TypeReference=新的TypeReference(){};
Map=mapper.readValue(json,typeReference);
系统输出打印项次(map);
返回图;
}
公共void write(HashMap mp)引发IOException{
//TODO自动生成的方法存根
Writer nodeWriter=Writer;
迭代器it=mp.entrySet().Iterator();
while(it.hasNext()){
Map.Entry对=(Map.Entry)it.next();
System.out.println(pair.getKey()+“=”+pair.getValue());
如果(对!=null){
nodeWriter.write(pair.getValue().toString());
}
nodeWriter.write(“,”);
}
writer.flush();
}
输出应写入文件为。。。
------------------------------------
价值1,价值2
键1-value1,键2-value2

不要从pair.getValue()调用toString()。而是调用自己的方法,在那里可以检查类并创建所需的输出