You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Jackson修改JSON中指定条件的节点字段值

使用Jackson修改指定JSON节点值

原始JSON内容

{
    "employeeIdentifiers": [
        {
            "type": "EmployeeProductID",
            "finalIdentifier": "464646"
        },
        {
            "type": "EmployeeDomainID",
            "finalIdentifier": "2424225"
        }
    ],
    "employeeStatus": [
        {
            "employeeType": "Employee",
            "employeeSUBIdentifiers": [{
                "type": "mainID",
                "code": "10224242424"
            }]
        },
        {
            "employeeType": "Customer",
            "employeeSUBIdentifiers": [{
                    "type": "subordinateID",
                    "code": "468668686"
                },
                {
                    "type": "mainID",
                    "code": "468668686"
                }
            ]
        }
    ]
}

方法一:使用Jackson Tree Model(无需实体类)

适合临时修改JSON结构,无需预先定义实体类:

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class JsonModifyExample {
    public static void main(String[] args) throws Exception {
        String jsonStr = "你的原始JSON字符串";
        ObjectMapper mapper = new ObjectMapper();
        
        // 解析JSON为根节点ObjectNode
        ObjectNode rootNode = (ObjectNode) mapper.readTree(jsonStr);
        
        // 获取employeeStatus数组节点
        JsonNode statusArray = rootNode.get("employeeStatus");
        
        // 遍历数组找到目标元素
        for (JsonNode statusNode : statusArray) {
            if ("Employee".equals(statusNode.get("employeeType").asText())) {
                // 获取employeeSUBIdentifiers数组第一个元素并修改code
                ObjectNode subIdNode = (ObjectNode) statusNode.get("employeeSUBIdentifiers").get(0);
                subIdNode.put("code", "9924242424");
                break; // 找到目标后退出循环
            }
        }
        
        // 转回格式化后的JSON字符串
        String modifiedJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(rootNode);
        System.out.println(modifiedJson);
    }
}

方法二:使用POJO映射(适合固定JSON结构)

如果JSON结构固定,用实体类映射可读性更强:

1. 定义对应实体类

import java.util.List;

// 根实体类
public class EmployeeRoot {
    private List<EmployeeIdentifier> employeeIdentifiers;
    private List<EmployeeStatus> employeeStatus;

    // Getters & Setters
    public List<EmployeeIdentifier> getEmployeeIdentifiers() { return employeeIdentifiers; }
    public void setEmployeeIdentifiers(List<EmployeeIdentifier> employeeIdentifiers) { this.employeeIdentifiers = employeeIdentifiers; }
    public List<EmployeeStatus> getEmployeeStatus() { return employeeStatus; }
    public void setEmployeeStatus(List<EmployeeStatus> employeeStatus) { this.employeeStatus = employeeStatus; }
}

// EmployeeIdentifier实体类
public class EmployeeIdentifier {
    private String type;
    private String finalIdentifier;

    // Getters & Setters
    public String getType() { return type; }
    public void setType(String type) { this.type = type; }
    public String getFinalIdentifier() { return finalIdentifier; }
    public void setFinalIdentifier(String finalIdentifier) { this.finalIdentifier = finalIdentifier; }
}

// EmployeeStatus实体类
public class EmployeeStatus {
    private String employeeType;
    private List<EmployeeSUBIdentifier> employeeSUBIdentifiers;

    // Getters & Setters
    public String getEmployeeType() { return employeeType; }
    public void setEmployeeType(String employeeType) { this.employeeType = employeeType; }
    public List<EmployeeSUBIdentifier> getEmployeeSUBIdentifiers() { return employeeSUBIdentifiers; }
    public void setEmployeeSUBIdentifiers(List<EmployeeSUBIdentifier> employeeSUBIdentifiers) { this.employeeSUBIdentifiers = employeeSUBIdentifiers; }
}

// EmployeeSUBIdentifier实体类
public class EmployeeSUBIdentifier {
    private String type;
    private String code;

    // Getters & Setters
    public String getType() { return type; }
    public void setType(String type) { this.type = type; }
    public String getCode() { return code; }
    public void setCode(String code) { this.code = code; }
}

2. 编写修改逻辑代码

import com.fasterxml.jackson.databind.ObjectMapper;

public class PojoModifyExample {
    public static void main(String[] args) throws Exception {
        String jsonStr = "你的原始JSON字符串";
        ObjectMapper mapper = new ObjectMapper();
        
        // 解析JSON为POJO对象
        EmployeeRoot root = mapper.readValue(jsonStr, EmployeeRoot.class);
        
        // 遍历找到目标EmployeeStatus对象
        for (EmployeeStatus status : root.getEmployeeStatus()) {
            if ("Employee".equals(status.getEmployeeType())) {
                // 修改第一个subIdentifier的code值(添加空判断避免NPE)
                if (!status.getEmployeeSUBIdentifiers().isEmpty()) {
                    status.getEmployeeSUBIdentifiers().get(0).setCode("9924242424");
                }
                break;
            }
        }
        
        // 转回格式化后的JSON字符串
        String modifiedJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(root);
        System.out.println(modifiedJson);
    }
}

注意事项

  • 需引入Jackson核心依赖(Maven示例:com.fasterxml.jackson.core:jackson-databind:2.x.x)
  • 处理数组时建议添加空值判断,避免空指针异常

内容的提问来源于stack exchange,提问作者user739115

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 13:55:36