如何将Java对象转换为JSONObject?

24 浏览
0 Comments

如何将Java对象转换为JSONObject?

我需要把一个POJO对象转换为JSONObject(org.json.JSONObject)。

我知道如何将它转换为文件:

ObjectMapper mapper = new ObjectMapper();
try {
    mapper.writeValue(new File(file.toString()), registrationData);
} catch (JsonGenerationException e) {
    e.printStackTrace();
} catch (JsonMappingException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

但这次我不想要一个文件。

0