导入的maven依赖
<!--json-->
<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.10.0</version>
</dependency>
示例代码
json要是String类型
public static void main(String[] args) throws Exception {String requestJson1 = "{\n" +" \"header\": {\n" +" \"app_id\": \"" + appid + "\",\n" +" \"status\":2\n" +" },\n" +" \"parameter\": {\n" +" \"s15282f39\": {\n" +" \"category\": \"ch_en_public_cloud\",\n" +" \"result\": {\n" +" \"encoding\": \"utf8\",\n" +" \"compress\": \"raw\",\n" +" \"format\": \"plain\"\n" +" }\n" +" },\n" +" \"s5eac762f\": {\n" +" \"result_type\": \"" + RESULT_TYPE + "\",\n" +" \"result\": {\n" +" \"encoding\": \"utf8\",\n" +" \"compress\": \"raw\",\n" +" \"format\": \"plain\"\n" +" }\n" +" }\n" +" },\n" +" \"payload\": {\n" +" \"test\": {\n" +" \"encoding\": \"" + "a" + "\",\n" +" \"image\": \"" + "11" + "\",\n" + " \"status\": 3\n" + " }\n" + " }\n" + "}";prettyPrint(requestJson1);ObjectMapper mapper = new ObjectMapper(); Object obj = mapper.readValue(requestJson1, Object.class);System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj));}
}
输出效果的json示例