jackson 提供了非常有用且轻量级的 API 来将 Object 转换为 JSON,反之亦然。请找到下面的示例代码来执行操作
List<Output> outputList = new ArrayList<Output>();
public static void main(String[] args) {
try {
Output output = new Output(1,"2342");
ObjectMapper objectMapper = new ObjectMapper();
String jsonString = objectMapper.writeValueAsString(output);
System.out.println(jsonString);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
Jackson API 还有许多其他功能和不错的文档。您可以参考以下链接:https ://www.journaldev.com/2324/jackson-json-java-parser-api-example-tutorial ..
要包含在项目中的依赖项是
<!-- Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.5.1</version>
</dependency>