从Map <Object,Object>创建Json字符串

时间:2019-07-19 10:23:48

标签: java json

我创建了2个类,像这样的Class1和Class2:

Class1{
   String a;
   String b;
   String c;
}

Class2{
   String d;
   String e;
   String f;
}

以这种方式创建地图:

Map<Class1, Class2> map1 = new LinkedHashMap<Class1, Class2>();

map1.put(new Class1("Apple", "Ball", "Cat"),
         new Class2("Dog","Elephant","Frog"));

我正在像这样转换此地图:

public static String convertMapToJson(Map input) {
    if (input == null || input.size() == 0)
        return null;
    ObjectMapper mapper = new ObjectMapper();
    String json = "";        try {
        json = mapper.writeValueAsString(input);
    } catch (Exception e) {
        logger.finer(e.getMessage());
    }
    return json;
}

我希望Json字符串为:

"a":"Apple","b":"Ball","c":"Cat"{
  "d":"Dog",
  "e":"Elephant",
  "f":"Frog"
}

但是我看到与“对象名称”相同的输出,我不确定为什么密钥没有转换为Json

"Object Name@12353"{
  "d":"Dog",
  "e":"Elephant",
  "f":"Frog"
}

0 个答案:

没有答案