1. Overview
1.概述
Working with JSON data in Java can be easy, but – like most anything in Java – there are a lot of options and libraries we can chose from.
在Java中处理JSON数据可以很容易,但是–就像Java中的大多数东西一样–有很多选项和库可供我们选择。
This guide should make that choice easier and should give you a solid understanding of the ecosystem right now. We’ll discuss the most common JSON processing libraries in Java:
本指南应使这种选择更容易,并应使你对现在的生态系统有一个坚实的了解。我们将讨论Java中最常见的JSON处理库。
We’re following a simple structure for each library – first some useful resources to get started (both here on Baeldung as well as external). Then we’re going to go over a basic code example, just to see how working with the library actually looks like.
我们对每个库都有一个简单的结构–首先是一些有用的资源,以便开始使用(包括在Baeldung这里以及外部)。然后,我们将讨论一个基本的代码例子,以了解使用该库的实际情况。
2. Popularity and Basic Stats
2.受欢迎程度和基本统计数字
First, let’s start with some statistics as a proxy for the popularity of each library:
首先,让我们从一些统计数据开始,作为每个图书馆受欢迎程度的代表。
2.1. Jackson
2.1.Jackson
2.2. Gson
2.2 Gson
2.3. json-io
2.3. json-io
2.4. Genson
2.4.Genson
3. Jackson
3.Jackson
Next, let’s have a look at the most popular of these – Jackson. Jackson is a multi-purpose Java library for processing JSON data.
接下来,让我们看看其中最流行的–杰克逊。Jackson是一个多用途的Java库,用于处理JSON数据。
3.1. Useful Resources
3.1.有用的资源
Here are some official resources for the library:
这里有一些图书馆的官方资源。
On Baeldung:
关于Baeldung:。
- Jackson tutorial
- Jackson Date
- Jackson JSON Views
- A Guide to Jackson Annotations
- Jackson Exceptions – Problems and Solutions
- Jackson – Decide What Fields Get Serialized/Deserialized
- Jackson – Bidirectional Relationships
- Jackson – Custom Serializer
- Jackson – Custom Deserializer
Other Interesting Writeups:
其他有趣的写法:。
- Jackson JSON Processing API in Java Example Tutorial
- Jackson – ObjectMapper
- Jackson 2 – Convert Java Object to / from JSON
3.2. Maven Dependency
3.2.Maven的依赖性
To use the library – here’s the Maven dependency to add into your pom.xml:
要使用该库–这里有Maven的依赖性,要添加到你的pom.xml中。
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
Note that the latest version of Jackson right now is 2.13.
请注意,Jackson的最新版本现在是2.13。
3.3. Simple Example with Jackson
3.3.使用Jackson的简单例子
Now, let’s see how to use Jackson in a simple example:
现在,让我们看看如何在一个简单的例子中使用杰克逊。
@Test
public void whenSerializeAndDeserializeUsingJackson_thenCorrect()
throws IOException{
Foo foo = new Foo(1,"first");
ObjectMapper mapper = new ObjectMapper();
String jsonStr = mapper.writeValueAsString(foo);
Foo result = mapper.readValue(jsonStr, Foo.class);
assertEquals(foo.getId(),result.getId());
}
Note that:
请注意,。
- ObjectMapper.writeValueAsString() is used to serialize Object to JSON string.
- ObjectMapper.readValue() is used to deserialize JSON string to Java Object.
- Sample JSON output:
{
"id":1,
"name":"first"
}
4. Gson
4.格森
Gson is the next Java JSON library that we’re going to be looking at.
Gson是我们接下来要研究的Java JSON库。
4.1. Useful Resources
4.1.有用的资源
Here are some official resources for the library:
这里有一些图书馆的官方资源。
On Baeldung:
关于Baeldung:。
Other Interesting Writeups:
其他有趣的写法:。
4.2. Maven Dependency
4.2.Maven的依赖性
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
Note that the latest version of Gson right now is 2.8.8.
请注意,Gson现在的最新版本是2.8.8。
4.3. Simple Example with Gson
4.3.使用Gson的简单例子
Here is a simple example clarify how to use Gson to serialize/desrialize JSON:
下面是一个简单的例子,说明如何使用Gson对JSON进行序列化/去工业化。
@Test
public void whenSerializeAndDeserializeUsingGson_thenCorrect(){
Gson gson = new Gson();
Foo foo = new Foo(1,"first");
String jsonStr = gson.toJson(foo);
Foo result = gson.fromJson(jsonStr, Foo.class);
assertEquals(foo.getId(),result.getId());
}
Note that:
请注意,。
- Gson.toJson() is used to serialize Object to JSON
- Gson.fromJson() is used to desrialize JSON to Java Object
5. Json-io
5.Json-io
Json-io is a simple Java library to serialize/deserialize JSON.
Json-io是一个简单的Java库,用于序列化/反序列化JSON。
5.1. Useful Resources
5.1.有用的资源
Here are some official resources for the library:
这里有一些图书馆的官方资源。
5.2. Maven Dependency
5.2.Maven的依赖性
<dependency>
<groupId>com.cedarsoftware</groupId>
<artifactId>json-io</artifactId>
<version>${json-io.version}</version>
</dependency>
Note that the latest version of json-io right now is 4.13.0.
请注意,json-io的最新版本现在是4.13.0。
5.3. Simple Example with json-io
5.3.使用json-io的简单例子
Now, let’s take a look at a simple example of using json-io:
现在,让我们看一下使用json-io的一个简单例子。
@Test
public void whenSerializeAndDeserializeUsingJsonio_thenCorrect(){
Foo foo = new Foo(1,"first");
String jsonStr = JsonWriter.objectToJson(foo);
Foo result = (Foo) JsonReader.jsonToJava(jsonStr);
assertEquals(foo.getId(),result.getId());
}
Note that:
请注意,。
- JsonWriter.objectToJson() is used to serialize Object to JSON.
- JsonReader.jsonToJava() is used to deserialize Json to Java Object.
- Sample JSON output:
{
"@type":"org.baeldung.Foo",
"id":1,
"name":"first"
}
6. Genson
6.Genson
Genson is a Java and Scala to JSON conversion library, providing full databinding and streaming.
Genson是一个Java和Scala到JSON的转换库,提供完整的数据绑定和流。
6.1. Useful Resources
6.1.有用的资源
Here are some official resources for the library:
这里有一些图书馆的官方资源。
6.2. Maven Dependency
6.2.Maven的依赖性
<dependency>
<groupId>com.owlike</groupId>
<artifactId>genson</artifactId>
<version>${genson.version}</version>
</dependency>
Note that the latest version of Genson right now is 1.6.
请注意,Genson的最新版本现在是1.6。
6.3. Simple Example with Genson
6.3.使用Genson的简单例子
Here’s a simple example of working with the library:
下面是一个使用该库的简单例子。
@Test
public void whenSerializeAndDeserializeUsingGenson_thenCorrect(){
Genson genson = new Genson();
Foo foo = new Foo(1,"first");
String jsonStr = genson.serialize(foo);
Foo result = genson.deserialize(jsonStr, Foo.class);
assertEquals(foo.getId(),result.getId());
}
Note that:
请注意,。
- Genson.serialize() is used to serialize Object to JSON
- Genson.desrialize() is used to deserialize JSON to Java Object
7. Conclusion
7.结论
In this quick overview article, we learned about the most common JSON processing libraries in Java.
在这篇快速概述文章中,我们了解了Java中最常见的JSON处理库。