我正在尝试使用一些数据创建复杂的JSon输出文件(一个大的HashTable集合),并在序列化输出时遇到一些错误。经过一些调查发现需要将Visibility设置为这些属性字段,现在我在输出中看到HastTable的奇怪值,实际上我不需要它们。这是一些代码:
PrintWriter out = null;
Map<String, String> hm = new HashMap<String, String>();
Map<String, String> hmReqMap = new HashMap<String, String>();
Map<String, String> hmResMap = new HashMap<String, String>();
Map<String, String> hmGate = new HashMap<String, String>();
Map<String, String> hmResult = new HashMap<String, String>();
JSONArray ja = new JSONArray();
JSONObject mainObj = new JSONObject();
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
try {
File file = new File(outputDirectory + File.separator + "aaa" + IConst.JSON_EXTENSION);
out = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));
if (tcStats != null) {
for (SingleTest singleTest : tcStats.getTestsList()) {
Map<String, String> reqMap = new HashMap<String, String>();
Map<String, String> resMap = new HashMap<String, String>();
SomeClassAllDetails allDdetails = singleTest.getSiteTransaction();
for (Entry<String, String> entry : allDetails.getEPowerEntries().entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
if (key.startsWith("req.")) {
reqMap.put(key.substring(4), value);
} else if (key.startsWith("res.")) {
resMap.put(key.substring(4), value);
}
}
hmGate.put("Gate", singleTest.getGate().toString());
hmReqMap.put("Request", reqMap.toString().substring(1, reqMap.toString().length() - 1));
hmResMap.put("Response", resMap.toString().substring(1, resMap.toString().length() - 1));
hmResult.put("Result", singleTest.getComparisonResult().toString());
hm.putAll(hmGate);
hm.putAll(hmReqMap);
hm.putAll(hmResMap);
hm.putAll(hmResult);
ja.put(hm);
mainObj.put(singleTest.getLabel(), ja);
}
String mapAsJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(mainObj);
out.println(mapAsJson);
out.println(",");
} else if ("opening".equals(type)) {
out.println("{");
} else if ("closing".equals(type)) {
out.println("}");
}
这是实际的输出,你可以看到像map,myArrayList这样的键在这里添加:
{
"map" : {
"Tran_ID" : {
"myArrayList" : [ {
"map" : {
"Response" : "value1=333333 value2=33333, value4=5555",
"Request" : "value6=44444, value7=000000",
"Gate" : "10.0.0.1",
"Result" : "Done"
}
} ]
}
}
}
如何删除它们? 如果我删除以下代码
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
然后我收到错误,我需要禁用SerializationFeature.FAIL_ON_EMPTY_BEANS
我的目标是创建这个Json输出:
{
"Tran_ID" : [{
"Response" : "value1=333333 value2=33333, value4=5555",
"Request" : "value6=44444, value7=000000",
"Gate" : "10.0.0.1",
"Result" : "Done"
}]
}
答案 0 :(得分:0)
好的,这是一个解决方案:
示例“填充”代码(生成的类名称):
TranID tranId = new TranID(); // {}
tranId.setResponse("value1=333333, value2=33333, value4=5555"); // { "Response": ... }
tranId.setRequest("value6=44444, value7=000000"); // { "Response": ..., "Request": ... }
tranId.setGate("10.0.0.1"); // etc
tranId.setResult("Done")
Example example = new Example(); // { "tranID": undefined }
example.setTranId(new ArrayList<>()); // { "tranID": [] }
example.getTranId().add(tranId); // { "tranID": [{ "Response": ..., "Request": ... }] }
// output
String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(example);
答案 1 :(得分:0)
感谢您的帮助,但这对问题没有帮助 我重新编写了代码并把它放到了这个:
String jo = Json.createObjectBuilder()
.add(singleTest.getLabel(), Json.createObjectBuilder()
.add("Request", reqMap.toString().substring(1, reqMap.toString().length() - 1))
.add("Response", resMap.toString().substring(1, resMap.toString().length() - 1))
.add("Result", singleTest.getComparisonResult().toString())
.add("Gate", singleTest.getGate().toString()))
.build().toString();
您还应该向pom.xml添加这些依赖项:
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.0.4</version>
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>1.0</version>
然后你可以用绳子做任何你想做的事。
由于