Java:Jackson缩进添加了一个新的映射标记
我使用的是Java:Jackson缩进添加了一个新的映射标记,java,json,indentation,jackson-databind,Java,Json,Indentation,Jackson Databind,我使用的是jackson2.9.8,我试图使用我的json 我使用的代码是: protected void setSuccessMessage(HttpServletResponse response, JSONObject jObj) throws IOException { // Set the status response.setStatus(200); // Create the response response.set
jackson2.9.8
,我试图使用我的json
我使用的代码是:
protected void setSuccessMessage(HttpServletResponse response, JSONObject jObj) throws IOException {
// Set the status
response.setStatus(200);
// Create the response
response.setContentType("application/json");
PrintWriter out = response.getWriter();
jObj.put("success", 1);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
mapper.setVisibility(PropertyAccessor.ALL, Visibility.ANY);
out.print(mapper.writeValueAsString(jObj));
out.close();
}
但是,我的输出有一个新的map
标记,这是我不想要的。输出为:
{
"map" : {
"success" : 1,
"documents_metata" : {
"myArrayList" : [ {
"map" : {
"documentType" : "PS_XML",
"patientId" : "x",
"effectiveTime" : "2019-05-08",
"author" : "xxx",
"repositoryId" : "xxx",
"id" : "xxx",
"title" : "xxx"
}
}, {
"map" : {
"documentType" : "PS_PDF",
"patientId" : "x",
"effectiveTime" : "2019-05-08",
"author" : "xxx",
"repositoryId" : "xxx",
"id" : "xxx",
"title" : "xxx"
}
} ]
}
}
}
正确的答案应该是:
{
"success": 1,
"documents_metadata": [
[
{
"documentType": "PS_PDF",
"patientId": "x",
"effectiveTime": "2019-05-08",
"author": "xxx",
"repositoryId": "xxx",
"id": "xxx",
"title": "xxx"
},
{
"documentType": "PS_XML",
"patientId": "x",
"effectiveTime": "2019-05-08",
"author": "xxx",
"repositoryId": "xxx",
"id": "xxx",
"title": "xxx"
}
]
]
}
没有
jackson
的json很好,但是没有缩进。你知道如何解决这个问题吗?Jackson对另一个库中的JSONObject
一无所知。所以它像其他类一样编写内部结构。用于告诉Jackson如何治疗:
import com.fasterxml.jackson.datatype.jsonorg.JsonOrgModule;
mapper.registerModule(new JsonOrgModule());
或者使用Jackson自己的(参见教程)。JSONObject来自
org.json.simple.JSONObject
?@michalk来自org.json.JSONObject使用lava.lang.Map或自定义类。Jackson被设计为在不使用低级实现(JsonNode)的情况下操作标准对象和类。