Backend Development 9 min read

Six Common JSON Parsing Methods in Java

Java developers can parse JSON using six popular approaches—Jackson for high‑performance, annotation‑driven serialization; Gson for a lightweight, easy‑to‑use API; FastJSON for speed; JsonPath for XPath‑style nested extraction; org.json for simple utility; or manual parsing for full control—each suited to different performance and complexity needs.

Java Tech Enthusiast
Java Tech Enthusiast
Java Tech Enthusiast
Six Common JSON Parsing Methods in Java

In Java development, parsing JSON is a common requirement for interacting with front‑end, third‑party APIs or configuration files.

1. Jackson – industry standard

Features: powerful serialization/deserialization, supports complex structures, annotation‑driven control (e.g., @JsonIgnore, @JsonProperty), high performance.

import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonExample {
    public static void main(String[] args) throws Exception {
        String json = "{\"id\":1,\"name\":\"张三\"}";
        ObjectMapper objectMapper = new ObjectMapper();
        User user = objectMapper.readValue(json, User.class);
        System.out.println(user.getName()); // 张三
    }
}
class User {
    private int id;
    private String name;
    // getters and setters omitted
}

2. Gson – lightweight and easy

Features: small footprint, generic support, annotation @Expose, extensible via custom serializers.

import com.google.gson.Gson;
public class GsonExample {
    public static void main(String[] args) {
        String json = "{\"id\":1,\"name\":\"王五\"}";
        Gson gson = new Gson();
        User user = gson.fromJson(json, User.class);
        System.out.println(user.getName()); // 王五
    }
}

3. FastJSON – high performance

Features: extremely fast parsing, supports dynamic fields and complex types, annotation control.

import com.alibaba.fastjson.JSON;
public class FastJsonExample {
    public static void main(String[] args) {
        String json = "{\"id\":1,\"name\":\"小明\"}";
        User user = JSON.parseObject(json, User.class);
        System.out.println(user.getName()); // 小明
    }
}

4. JsonPath – quick nested field extraction

Features: XPath‑like expressions for extracting nested values, lightweight.

import com.jayway.jsonpath.JsonPath;
public class JsonPathExample {
    public static void main(String[] args) {
        String json = "{\"store\":{\"book\":[{\"title\":\"书1\",\"price\":10},{\"title\":\"书2\",\"price\":20}]}}";
        String title = JsonPath.read(json, "$.store.book[0].title");
        System.out.println(title); // 书1
        List
prices = JsonPath.read(json, "$.store.book[*].price");
        System.out.println(prices); // [10, 20]
    }
}

5. org.json – simple utility

Features: lightweight, easy construction and extraction, limited to simple scenarios.

import org.json.JSONObject;
public class OrgJsonExample {
    public static void main(String[] args) {
        String json = "{\"id\":1,\"name\":\"张三\"}";
        JSONObject jsonObject = new JSONObject(json);
        System.out.println(jsonObject.getString("name")); // 张三
        JSONObject newJson = new JSONObject();
        newJson.put("id", 2);
        newJson.put("name", "李四");
        System.out.println(newJson.toString()); // {"id":2,"name":"李四"}
    }
}

6. Manual parsing – maximum flexibility

Features: no third‑party library, full control over dynamic JSON structures.

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
public class ManualParsing {
    public static void main(String[] args) throws Exception {
        String json = "{\"id\":1,\"name\":\"动态字段\"}";
        ObjectMapper objectMapper = new ObjectMapper();
        Map
map = objectMapper.readValue(json, new TypeReference
>() {});
        System.out.println(map.get("name")); // 动态字段
    }
}

Each method has its own strengths and weaknesses; choose the library that best fits the project’s performance, complexity and maintenance requirements.

JavaParsingJSONfastjsonGsonJacksonJsonPath
Java Tech Enthusiast
Written by

Java Tech Enthusiast

Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.