杰瑞科技汇

Java对象与JSON如何高效转换?

Of course! This is a fundamental concept in modern Java development. Converting between Java objects and JSON (JavaScript Object Notation) is essential for web APIs, configuration files, and data storage.

Here's a comprehensive guide covering the most popular and practical ways to handle this in Java.

What is JSON?

JSON is a lightweight, text-based data-interchange format. It's easy for humans to read and write, and easy for machines to parse and generate. It's a key part of most modern web applications (especially REST APIs).

A simple JSON object looks like this:

{
  "name": "John Doe",
  "age": 30,
  "isStudent": false,
  "courses": [
    { "title": "History", "credits": 3 },
    { "title": "Math", "credits": 4 }
  ]
}

The Core Concepts: Serialization and Deserialization

  • Serialization (Java Object → JSON String): The process of converting a Java object into a JSON string. This is done so you can send the object over a network or save it to a file.
  • Deserialization (JSON String → Java Object): The process of reading a JSON string and converting it back into a Java object. This allows you to work with the data in your Java code using its proper structure and types.

Method 1: Jackson (The Industry Standard)

Jackson is the de-facto standard library in the Java ecosystem. It's powerful, feature-rich, and has excellent performance. It's used by the Spring Framework and many other major projects.

Add the Dependency

You need to add the Jackson Databind library to your project.

Maven (pom.xml):

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.15.2</version> <!-- Use the latest version -->
</dependency>

Gradle (build.gradle):

implementation 'com.fasterxml.jackson.core:jackson-databind:2.15.2' // Use the latest version

Create a Java Class (POJO)

Your Java object needs to match the structure of the JSON. The fields should have getters and setters. Jackson can automatically map JSON keys to Java fields.

// User.java
import java.util.List;
public class User {
    private String name;
    private int age;
    private boolean isStudent;
    private List<Course> courses;
    // Getters and Setters are crucial!
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public int getAge() { return age; }
    public void setAge(int age) { this.age = age; }
    public boolean isStudent() { return isStudent; } // Note: 'is' prefix for boolean
    public void setStudent(boolean student) { isStudent = student; }
    public List<Course> getCourses() { return courses; }
    public void setCourses(List<Course> courses) { this.courses = courses; }
    // Optional: A toString() method for easy printing
    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", isStudent=" + isStudent +
                ", courses=" + courses +
                '}';
    }
}
// Course.java
public class Course {
    private String title;
    private int credits;
    // Getters and Setters
    public String getTitle() { return title; }
    public void setTitle(String title) { this.title = title; }
    public int getCredits() { return credits; }
    public void setCredits(int credits) { this.credits = credits; }
    @Override
    public String toString() {
        return "Course{" +
                "title='" + title + '\'' +
                ", credits=" + credits +
                '}';
    }
}

Perform Serialization (Object to JSON)

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonSerializationExample {
    public static void main(String[] args) {
        // 1. Create an ObjectMapper instance
        ObjectMapper objectMapper = new ObjectMapper();
        // 2. Create a Java object
        User user = new User();
        user.setName("John Doe");
        user.setAge(30);
        user.setStudent(false);
        List<Course> courses = List.of(
            new Course("History", 3),
            new Course("Math", 4)
        );
        user.setCourses(courses);
        try {
            // 3. Serialize the object to a JSON string
            // The 'true' argument makes the JSON output pretty (formatted)
            String jsonString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(user);
            System.out.println("JSON String:");
            System.out.println(jsonString);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
}

Output:

JSON String:
{
  "name" : "John Doe",
  "age" : 30,
  "isStudent" : false,
  "courses" : [ { : "History",
    "credits" : 3
  }, { : "Math",
    "credits" : 4
  } ]
}

Perform Deserialization (JSON to Object)

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonDeserializationExample {
    public static void main(String[] args) {
        String jsonString = """
        {
          "name" : "Jane Doe",
          "age" : 28,
          "isStudent" : true,
          "courses" : [ {
            "title" : "Biology",
            "credits" : 4
          } ]
        }
        """;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            // Deserialize the JSON string into a User object
            User user = objectMapper.readValue(jsonString, User.class);
            System.out.println("Java Object:");
            System.out.println(user); // Uses the toString() method
            System.out.println("User's first course: " + user.getCourses().get(0).getTitle());
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
}

Output:

Java Object:
User{name='Jane Doe', age=28, isStudent=true, courses=[Course{title='Biology', credits=4}]}
User's first course: Biology

Method 2: Gson (A Great Alternative from Google)

Gson is another extremely popular and robust library from Google. It's very similar to Jackson in its usage.

Add the Dependency

Maven (pom.xml):

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.10.1</version> <!-- Use the latest version -->
</dependency>

Serialization (Object to JSON)

You can use the same User.java class as before.

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class GsonSerializationExample {
    public static void main(String[] args) {
        // 1. Create a Gson instance (with pretty printing)
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        // 2. Create a Java object (same as Jackson example)
        User user = new User();
        user.setName("John Doe");
        user.setAge(30);
        user.setStudent(false);
        // ... (set courses as before)
        // 3. Serialize the object
        String jsonString = gson.toJson(user);
        System.out.println("JSON String (Gson):");
        System.out.println(jsonString);
    }
}

Deserialization (JSON to Object)

import com.google.gson.Gson;
public class GsonDeserializationExample {
    public static void main(String[] args) {
        String jsonString = """
        {
          "name" : "Jane Doe",
          "age" : 28,
          "isStudent" : true,
          "courses" : [ {
            "title" : "Biology",
            "credits" : 4
          } ]
        }
        """;
        Gson gson = new Gson();
        // Deserialize the JSON string
        User user = gson.fromJson(jsonString, User.class);
        System.out.println("Java Object (Gson):");
        System.out.println(user);
    }
}

Method 3: Using the Built-in javax.json (Java EE / Jakarta EE)

If you're working in a Java EE or Jakarta EE environment (like a Jakarta EE server), you might have access to the javax.json API. It's more verbose than Jackson or Gson but doesn't require external libraries for basic use.

This API works with a parser model (JsonParser) or an object model (JsonObject, JsonArray).

Example: Parsing JSON with JsonParser

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonReader;
import java.io.StringReader;
public class JavaJsonApiExample {
    public static void main(String[] args) {
        String jsonString = """
        {
          "name" : "Peter Jones",
          "age" : 45,
          "isStudent" : false
        }
        """;
        try (JsonReader reader = Json.createReader(new StringReader(jsonString))) {
            // The entire JSON is read as a JsonObject
            JsonObject jsonObject = reader.readObject();
            // Get values by key (with type-specific methods)
            String name = jsonObject.getString("name");
            int age = jsonObject.getInt("age");
            boolean isStudent = jsonObject.getBoolean("isStudent");
            System.out.println("Name: " + name);
            System.out.println("Age: " + age);
            System.out.println("Is Student: " + isStudent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Comparison and Recommendation

Feature Jackson Gson javax.json (Built-in)
Popularity Very High (Industry Standard) Very High Moderate (Java EE specific)
Performance Excellent Excellent Good
Ease of Use Very Easy (annotations are powerful) Very Easy Verbose (more manual parsing)
Dependencies Requires external library (jackson-databind) Requires external library (gson) None (part of Java EE/Jakarta EE)
Key Strength Power, flexibility, features, integration Simplicity, good documentation No external dependencies
Best For Most projects, especially Spring apps. Projects where Google support is a plus. Simple tasks or when you can't add libs.

Final Recommendation

  • For almost any new project in Java, use Jackson. It's the most widely adopted, has the best performance, and is deeply integrated into the most popular frameworks like Spring Boot.
  • If you have a specific reason to prefer Google's library or find its API slightly more intuitive, Gson is an equally excellent choice.
  • Use the built-in javax.json only if you are in a constrained environment where you cannot add third-party libraries and your JSON parsing needs are simple.
分享:
扫描分享到社交APP
上一篇
下一篇