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

如何基于Jackson的JsonNode实现指定JSON结构转换?

使用Jackson将JsonNode转换为目标JSON结构

核心思路

直接复用原输入JSON作为结果的data字段,重点遍历index下的mutlipleIndices和simpleIndices两个数组,将每个条目转换为flatData数组中的对象:

  • 提取每个条目的index值作为key
  • 根据值的类型(数组、字符串、数字)确定type和对应的value_xxx字段
  • 拼接key_type为key.type格式

实现步骤

  1. 初始化Jackson工具类:用ObjectMapper处理JSON节点的创建与转换
  2. 提取输入中的目标节点:获取index下的mutlipleIndices和simpleIndices数组
  3. 遍历并转换每个条目:
    • 兼容字段拼写差异(如valeurs替代values)
    • 判断值的类型,匹配对应的输出字段
  4. 构建最终输出结构:将原输入放入data字段,转换后的列表放入flatData数组

完整代码示例

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

public class JsonTransformer {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    public static JsonNode transform(JsonNode inputNode) {
        // 创建根节点
        ObjectNode resultNode = objectMapper.createObjectNode();
        // 放入原输入到data字段
        resultNode.set("data", inputNode);

        // 创建flatData数组
        ArrayNode flatDataArray = objectMapper.createArrayNode();
        resultNode.set("flatData", flatDataArray);

        // 获取index节点
        JsonNode indexNode = inputNode.get("index");
        if (indexNode == null) {
            return resultNode;
        }

        // 处理mutlipleIndices数组
        processIndexArray(indexNode.get("mutlipleIndices"), flatDataArray);
        // 处理simpleIndices数组
        processIndexArray(indexNode.get("simpleIndices"), flatDataArray);

        return resultNode;
    }

    private static void processIndexArray(JsonNode arrayNode, ArrayNode flatDataArray) {
        if (arrayNode == null || !arrayNode.isArray()) {
            return;
        }

        for (JsonNode itemNode : arrayNode) {
            String key = itemNode.get("index").asText();
            // 兼容values/valeurs字段拼写差异
            JsonNode valueNode = itemNode.has("values") ? itemNode.get("values") : itemNode.get("valeurs");
            if (valueNode == null) {
                continue;
            }

            ObjectNode flatItem = objectMapper.createObjectNode();
            flatItem.put("key", key);

            if (valueNode.isArray()) {
                // 数组类型默认按string处理
                flatItem.put("type", "string");
                flatItem.put("key_type", key + ".string");
                flatItem.set("value_string", valueNode);
            } else if (valueNode.isTextual()) {
                // 字符串类型
                flatItem.put("type", "string");
                flatItem.put("key_type", key + ".string");
                flatItem.put("value_string", valueNode.asText());
            } else if (valueNode.isNumber()) {
                // 数字类型转为float
                flatItem.put("type", "float");
                flatItem.put("key_type", key + ".float");
                flatItem.put("value_float", valueNode.floatValue());
            }

            flatDataArray.add(flatItem);
        }
    }

    // 测试用例
    public static void main(String[] args) throws Exception {
        String inputJson = "{\"index\":{\"mutlipleIndices\":[{\"index\":\"languages\",\"values\":[\"English\",\"German\"]},{\"index\":\"editors\",\"values\":[\"AC. Renus\",\"Lodiga\"]}],\"simpleIndices\":[{\"index\":\"bookName\",\"values\":\"A song Of Ice and fire\"},{\"index\":\"nbPages\",\"valeurs\":1600}]}}";
        JsonNode inputNode = objectMapper.readTree(inputJson);
        JsonNode resultNode = transform(inputNode);
        // 格式化输出结果
        System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(resultNode));
    }
}

关键细节说明

  • 字段兼容:代码处理了values和valeurs两种字段名,避免拼写错误导致数据丢失
  • 类型适配:针对数组、字符串、数字三种类型分别处理,匹配目标结构的value_string和value_float字段
  • 扩展性:后续如需支持更多类型,只需在processIndexArray方法中添加对应类型判断逻辑即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 17:06:55