在java中处理json数据的核心是使用第三方库如jackson或gson进行序列化和反序列化,首先需定义与json结构对应的java pojo;2. 使用jackson时需在maven中引入jackson-databind依赖,通过objectmapper类的writevalueasstring()方法将java对象转换为json字符串(序列化);3. 通过objectmapper的readvalue()方法将json字符串转换为java对象(反序列化),支持字段缺失时自动设为null;4. jackson支持通过@jsonformat注解或objectmapper配置自定义日期格式,避免默认的时间戳输出;5. 使用@jsoninclude(jsoninclude.include.non_null)可忽略值为null的字段,减小json体积;6. 对于特殊类型如自定义money类,可通过实现jsonserializer和jsondeserializer并配合@jsonserialize和@jsondeserialize注解实现自定义序列化逻辑;7. 处理嵌套json对象时,java pojo中应包含对应类型的对象字段,jackson会自动递归映射;8. json数组可映射为java中的list或数组类型,字符串数组对应list<string>,对象数组对应list<pojo>,jackson能自动完成集合的序列化与反序列化。

在Java中处理JSON数据,无论是将其转换为JSON字符串(序列化)还是从JSON字符串中提取数据(反序列化),核心都是依赖于强大的第三方库。这通常涉及将Java对象与JSON结构进行映射,然后通过库的方法实现转换。最常用的库包括Jackson和Gson,它们提供了简洁而高效的API来完成这些任务。
要生成和解析JSON数据,我们通常会定义一个Java对象(POJO),其字段与JSON的键值对相对应。以Jackson库为例,以下是基本操作:
首先,确保你的项目中引入了Jackson的依赖。如果你使用Maven,可以在
pom.xml
立即学习“Java免费学习笔记(深入)”;
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.17.0</version> <!-- 使用最新稳定版本 -->
</dependency>1. 定义一个Java对象(POJO):
// User.java
public class User {
private String name;
private int age;
private String email; // 假设有时可能为空
// 默认构造函数是必须的,Jackson会用它来实例化对象
public User() {
}
public User(String name, int age, String email) {
this.name = name;
this.age = age;
this.email = email;
}
// Getter和Setter方法
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 String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", email='" + email + '\'' +
'}';
}
}2. 生成JSON(序列化):
将Java对象转换为JSON字符串。
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonGeneratorExample {
public static void main(String[] args) {
ObjectMapper objectMapper = new ObjectMapper();
User user = new User("张三", 30, "zhangsan@example.com");
try {
// 将Java对象序列化为JSON字符串
String jsonString = objectMapper.writeValueAsString(user);
System.out.println("生成的JSON字符串:\n" + jsonString);
// 也可以格式化输出,使其更易读
String prettyJsonString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(user);
System.out.println("\n格式化后的JSON字符串:\n" + prettyJsonString);
} catch (Exception e) {
e.printStackTrace();
}
}
}输出可能类似:
{"name":"张三","age":30,"email":"zhangsan@example.com"}3. 解析JSON(反序列化):
将JSON字符串转换回Java对象。
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonParserExample {
public static void main(String[] args) {
ObjectMapper objectMapper = new ObjectMapper();
String jsonString = "{\"name\":\"李四\",\"age\":25,\"email\":\"lisi@example.com\"}";
try {
// 将JSON字符串反序列化为Java对象
User user = objectMapper.readValue(jsonString, User.class);
System.out.println("解析后的Java对象:\n" + user);
// 尝试解析一个email字段缺失的JSON
String jsonWithoutEmail = "{\"name\":\"王五\",\"age\":40}";
User user2 = objectMapper.readValue(jsonWithoutEmail, User.class);
System.out.println("解析email缺失的Java对象:\n" + user2); // email会是null
} catch (Exception e) {
e.printStackTrace();
}
}
}输出可能类似:
解析后的Java对象: User{name='李四', age=25, email='lisi@example.com'}解析email缺失的Java对象: User{name='王五', age=40, email='null'}在Java生态中,处理JSON数据几乎离不开两个主流库:Jackson和Gson。作为开发者,选择哪个,有时确实是个让人纠结的问题。我的经验是,它们各有千秋,但Jackson在大多数企业级应用中表现更全面、更灵活。
Jackson Jackson是目前Java领域最流行的JSON处理库,功能非常强大。它不仅仅能做基本的序列化和反序列化,还提供了丰富的注解(如
@JsonProperty
@JsonIgnore
@JsonFormat
Gson Gson是Google开源的JSON库,以其API的简洁性著称。对于简单的JSON操作,或者当你的项目追求快速开发、对JSON映射的精细控制要求不高时,Gson是一个非常好的选择。它的学习曲线非常平缓,几行代码就能搞定基本的转换。然而,在处理一些复杂场景,比如多态、循环引用、或者需要高度定制化序列化逻辑时,Gson的灵活性相对Jackson来说会弱一些,可能需要更多的手动实现
JsonSerializer
JsonDeserializer
我的看法: 如果你正在启动一个新项目,并且预计未来会与各种复杂的API接口打交道,或者需要处理大量异构数据,我个人会倾向于推荐Jackson。它的生态系统更成熟,社区支持也更广泛,遇到问题更容易找到解决方案。当然,如果你的项目体量较小,或者只是做一些简单的JSON转换,Gson的简洁性无疑会让你感到愉悦。选择哪个,很多时候也取决于团队的偏好和项目的具体需求,没有绝对的对错,只有更适合。
在实际开发中,JSON数据的序列化和反序列化远不止基本类型那么简单。日期格式、空值处理以及一些特殊类型的映射,常常会成为让人头疼的细节。Jackson提供了强大的配置能力来应对这些挑战。
Easily find JSON paths within JSON objects using our intuitive Json Path Finder
30
1. 日期格式化: JSON本身没有日期类型,日期通常以字符串形式表示。常见的格式有ISO 8601 (
"yyyy-MM-dd'T'HH:mm:ss.SSSZ"
java.util.Date
java.time.LocalDateTime
你可以通过
@JsonFormat
import com.fasterxml.jackson.annotation.JsonFormat;
import java.util.Date;
public class Event {
private String name;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
private Date eventTime;
// 构造函数、Getter/Setter...
public Event() {}
public Event(String name, Date eventTime) {
this.name = name;
this.eventTime = eventTime;
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Date getEventTime() { return eventTime; }
public void setEventTime(Date eventTime) { this.eventTime = eventTime; }
}
// 序列化示例
// ObjectMapper mapper = new ObjectMapper();
// Event event = new Event("生日派对", new Date());
// String json = mapper.writeValueAsString(event); // eventTime将按"yyyy-MM-dd HH:mm:ss"格式输出或者通过
ObjectMapper
ObjectMapper mapper = new ObjectMapper();
// 配置日期格式
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
// 或者使用更现代的Java 8日期API
// mapper.registerModule(new JavaTimeModule()); // 注册Java 8日期模块
// mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); // 不写成时间戳2. 空值处理: 默认情况下,Jackson在序列化时会包含值为
null
null
你可以使用
@JsonInclude
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_NULL) // 类级别:所有null字段不参与序列化
public class Product {
private String productId;
private String productName;
private String description; // 可能会是null
// 构造函数、Getter/Setter...
public Product() {}
public Product(String productId, String productName, String description) {
this.productId = productId;
this.productName = productName;
this.description = description;
}
public String getProductId() { return productId; }
public void setProductId(String productId) { this.productId = productId; }
public String getProductName() { return productName; }
public void setProductName(String productName) { this.productName = productName; }
public String getDescription() { return description; }
public void setDescription(String description) { this.description = description; }
}
// 序列化示例
// ObjectMapper mapper = new ObjectMapper();
// Product p1 = new Product("P001", "电脑", "高性能笔记本");
// Product p2 = new Product("P002", "鼠标", null);
// String json1 = mapper.writeValueAsString(p1); // {"productId":"P001","productName":"电脑","description":"高性能笔记本"}
// String json2 = mapper.writeValueAsString(p2); // {"productId":"P002","productName":"鼠标"} (description字段被忽略)3. 自定义序列化与反序列化: 当默认的映射规则无法满足需求时,你可以实现自定义的序列化器和反序列化器。这在处理一些非标准数据类型、或者需要对数据进行特定转换时非常有用。
例如,如果你有一个自定义的
Money
100.50
{"amount":100.50,"currency":"USD"}import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import java.io.IOException;
import java.math.BigDecimal;
// 自定义Money类
class Money {
private BigDecimal amount;
private String currency;
public Money(BigDecimal amount, String currency) {
this.amount = amount;
this.currency = currency;
}
public BigDecimal getAmount() { return amount; }
public String getCurrency() { return currency; }
}
// 自定义Money的序列化器
class MoneySerializer extends JsonSerializer<Money> {
@Override
public void serialize(Money value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
// 只输出金额部分
gen.writeNumber(value.getAmount());
}
}
// 自定义Money的反序列化器
class MoneyDeserializer extends JsonDeserializer<Money> {
@Override
public Money deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
// 从数字反序列化为Money对象,假设货币是固定的USD
BigDecimal amount = p.getDecimalValue();
return new Money(amount, "USD");
}
}
// 在POJO中使用自定义序列化/反序列化器
public class Order {
private String orderId;
@JsonSerialize(using = MoneySerializer.class)
@JsonDeserialize(using = MoneyDeserializer.class)
private Money totalAmount;
// 构造函数、Getter/Setter...
public Order() {}
public Order(String orderId, Money totalAmount) {
this.orderId = orderId;
this.totalAmount = totalAmount;
}
public String getOrderId() { return orderId; }
public void setOrderId(String orderId) { this.orderId = orderId; }
public Money getTotalAmount() { return totalAmount; }
public void setTotalAmount(Money totalAmount) { this.totalAmount = totalAmount; }
}
// 示例
// ObjectMapper mapper = new ObjectMapper();
// Order order = new Order("ORD001", new Money(new BigDecimal("99.99"), "EUR"));
// String json = mapper.writeValueAsString(order); // {"orderId":"ORD001","totalAmount":99.99}
// Order parsedOrder = mapper.readValue("{\"orderId\":\"ORD002\",\"totalAmount\":123.45}", Order.class);
// System.out.println(parsedOrder.getTotalAmount().getCurrency()); // 输出 USD这种自定义机制为处理复杂或非标准的数据类型提供了极大的灵活性。
真实世界的JSON数据往往不是扁平的,它们经常包含嵌套的对象和数组。Jackson库能够非常优雅地处理这些复杂的结构,通常你只需要正确地定义你的Java POJO即可。
1. 处理嵌套对象: 如果JSON中包含一个嵌套的JSON对象,你只需要在你的Java POJO中定义一个对应类型的字段即可。Jackson会自动识别并进行递归映射。
假设我们有这样的JSON:
{
"orderId": "ORD123",
"customer": {
"id": "CUST001",
"name": "张三",
"contact": {
"email": "zhangsan@example.com",
"phone": "13800138000"
}
},
"totalAmount": 150.75
}你需要定义三个POJO来匹配这个结构:
public class Order {
private String orderId;
private Customer customer; // 嵌套对象
private double totalAmount;
// 构造函数、Getter/Setter
public Order() {}
public Order(String orderId, Customer customer, double totalAmount) {
this.orderId = orderId;
this.customer = customer;
this.totalAmount = totalAmount;
}
public String getOrderId() { return orderId; }
public void setOrderId(String orderId) { this.orderId = orderId; }
public Customer getCustomer() { return customer; }
public void setCustomer(Customer customer) { this.customer = customer; }
public double getTotalAmount() { return totalAmount; }
public void setTotalAmount(double totalAmount) { this.totalAmount = totalAmount; }
}
public class Customer {
private String id;
private String name;
private Contact contact; // 再次嵌套
// 构造函数、Getter/Setter
public Customer() {}
public Customer(String id, String name, Contact contact) {
this.id = id;
this.name = name;
this.contact = contact;
}
public String getId() { return id; }
public void setId(String id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Contact getContact() { return contact; }
public void setContact(Contact contact) { this.contact = contact; }
}
public class Contact {
private String email;
private String phone;
// 构造函数、Getter/Setter
public Contact() {}
public Contact(String email, String phone) {
this.email = email;
this.phone = phone;
}
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
public String getPhone() { return phone; }
public void setPhone(String phone) { this.phone = phone; }
}
// 反序列化示例
// ObjectMapper mapper = new ObjectMapper();
// String json = "{\"orderId\":\"ORD123\",\"customer\":{\"id\":\"CUST001\",\"name\":\"张三\",\"contact\":{\"email\":\"zhangsan@example.com\",\"phone\":\"13800138000\"}},\"totalAmount\":150.75}";
// Order order = mapper.readValue(json, Order.class);
// System.out.println(order.getCustomer().getContact().getEmail()); // 输出 zhangsan@example.com2. 处理JSON数组: JSON数组在Java中通常映射为
List
Set
假设我们有这样的JSON:
{
"productId": "PROD001",
"productName": "智能手机",
"tags": ["电子产品", "通讯", "数码"],
"variants": [
{
"color": "黑色",
"price": 999.00
},
{
"color": "白色",
"price": 1029.00
}
]
}对应的POJO结构:
import java.util.List;
public class ProductDetail {
private String productId;
private String productName;
private List<String> tags; // 字符串数组
private List<ProductVariant> variants; // 对象数组
// 构造函数、Getter/Setter
public ProductDetail() {}
public ProductDetail(String productId, String productName, List<String> tags, List<ProductVariant> variants) {
this.productId = productId;
this.productName = productName;
this.tags = tags;
this.variants = variants;
}
public String getProductId() { return productId; }
public void setProductId(String productId) { this.productId = productId; }
public String getProductName() { return productName; }
public void setProductName(String productName) { this.productName = productName; }
public List<String> getTags() { return tags; }
public void setTags(List<String> tags) { this.tags = tags; }
public List<ProductVariant> getVariants() { return variants; }
public void setVariants(List<ProductVariant> variants) { this.variants = variants; }
}
public class ProductVariant {
private String color;
private double price;
// 构造函数、Getter/Setter
public ProductVariant() {}
public ProductVariant(String color, double price) {
this.color = color;
this.price = price;
}
public String getColor() { return color; }
public void setColor(String color) { this.color = color; }
public double getPrice() { return price; }
public void setPrice(double price) { this.price = price; }
}
// 反序列化示例
// ObjectMapper mapper = new ObjectMapper();
// String json = "{\"productId\":\"PROD001\",\"productName\":\"智能手机\",\"tags\":[\"电子产品\",\"通讯\",\"数码\"],\"variants\":[{\"color\":\"黑色\",\"price\":999.00},{\"color\":\"白色\",\"price\":1029.00}]}";
// ProductDetail product = mapper.readValue(json, ProductDetail.class);
// System.out.println(product.getTags().get(0)); // 输出 电子产品
// System.out.println(product.getVariants().get(1).getColor()); // 输出 白色**3. 处理未知或动态结构
以上就是java怎样生成和解析JSON格式数据 javaJSON处理的入门编程指南的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号