从org.json JSONObject到org.codehaus.jackson [英] From org.json JSONObject to org.codehaus.jackson
问题描述
我想从 org.json
转移到 org.codehaus.jackson
。如何转换以下Java代码?
I want to move from org.json
to org.codehaus.jackson
. How do I convert the following Java code?
private JSONObject myJsonMessage(String message){
JSONObject obj = new JSONObject();
obj.put("message",message);
return obj;
}
我遗漏了 try-catch
为简单起见阻止。
I left out the try-catch
block for simplicity.
推荐答案
而不是 JSONObject
使用杰克逊的 ObjectMapper
和 ObjectNode
:
Instead of JSONObject
use Jackson's ObjectMapper
and ObjectNode
:
ObjectMapper mapper = new ObjectMapper();
ObjectNode node = mapper.createObjectNode();
node.put("message", "text");
这将是杰克逊相当于你当前的 org.json
代码。
This would be Jackson's equivalent of your current org.json
code.
然而,杰克逊真正擅长的是它能够在你的Java类(POJO)和它们的JSON表示之间进行复杂映射,以及它允许你的流API至少在与 org.json
的对应物进行比较时,要进行真正的快速序列化。
However, where Jackson really excels is in its capacity to do complex mappings between your Java classes (POJOs) and their JSON representation, as well as its streaming API which allows you to do really fast serialization, at least when compared with org.json
's counterparts.
这篇关于从org.json JSONObject到org.codehaus.jackson的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!