【御控工业物联网】JAVA JSON结构转换、JSON结构重构、JSON结构互换(5):对象To对象——转换映射方式

发布于:2024-04-25 ⋅ 阅读:(26) ⋅ 点赞:(0)

御控官网:https://www.yu-con.com/


一、JSON结构转换是什么?

JSON结构转换指的是将一个JSON对象或JSON数组按照一定规则进行重组、筛选、映射或转换,生成新的JSON对象或数组的过程。这种转换可以包括改变JSON数据的结构、提取特定字段、合并多个JSON数据,或者对数据进行计算和处理等操作。

在JSON结构转换中,常见的操作包括:

  • 提取字段:从一个JSON对象中提取特定字段,生成新的JSON对象。
  • 过滤数据:根据条件过滤JSON数据,生成符合条件的新JSON对象或数组。
  • 映射转换:将一个JSON对象中的字段映射到另一个字段,生成新的JSON对象。
  • 合并数据:将多个JSON对象或数组合并成一个新的JSON对象或数组。

JSON结构转换通常在数据处理、数据清洗、数据分析等场景中广泛应用。通过结构转换,可以根据需求定制化地处理JSON数据,使其符合特定的业务逻辑或数据格式要求。
为此我们提供了三个简单开源的类库(JavaScript类库Java类库.Net类库),接下来我们对Java类库进行详细讲解。

二、术语解释

1. 1.转换映射【高级配置选项】

转换映射包含两种

  • 交叉映射(默认)
    交叉映射主要是针对一对多、多对一、多对多的情况
  • 一对一映射
    一对一映射主要是针对一对一情况,此种情况需要注意,由于对象的属性每次重组后顺序无法控制,所以尽量通过属性名称进行精准映射

三、案例之《JSON对象 To JSON对象》

源JSON结构:

{
    "a": {
        "c": {
            "c_child": "2"
        },
        "d": {
            "d_child": "3"
        }
    }
}

目标JSON结构:

{
    "b1": {
        "k2": {
            "k2_child": "v2_child"
        },
        "k3": {
            "k3_child": "v3_child"
        }
    }
}

转换需求:

以下需求分别执行

  1. 一对多:将源结构的“a.c”值追加到目标结构的“b1.*”值
  2. 多对一:将源结构的“a.*”值追加到目标结构的“b1.k2”值
  3. 多对多:将源结构的“a.”值追加到目标结构的“b1.”值
  4. 一对一:将源结构的“a.”键追加到目标结构的“b1.”值

四、代码实现

1.一对多:将源结构的“a.c”值追加到目标结构的“b1.*”值


import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.List;

/**
 * 对象转换对象
 */
public class Main {

    public static void main(String[] args) {
        ObjectMapper mapper = new ObjectMapper();

        String orgJson = "{\"a\":{\"c\":{\"c_child\":\"2\"},\"d\":{\"d_child\":\"3\"}}}"; // JSON字符串
        String aimJson = "{\"b1\":{\"k2\":{\"k2_child\":\"v2_child\"},\"k3\":{\"k3_child\":\"v3_child\"}}}"; // JSON字符串


        List<JsonMapping> jsonMappings = new ArrayList<>();
        jsonMappings.add(new JsonMapping("root.b1.*", "root.a.c", 4,new JsonMappingOptions(0,1,1,1)));


        JsonTranferUtil jsonTranferUtil = null;
        String result ="";

        try {
            jsonTranferUtil = new JsonTranferUtil(orgJson, aimJson, jsonMappings);
            result = jsonTranferUtil.tranJson();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("******************结果 **********************");

        System.out.println(result);
    }
}

执行结果如下:
在这里插入图片描述

2.多对一:将源结构的“a.*”值追加到目标结构的“b1.k2”值

import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.List;
/**
 * 对象转换对象
 */
public class Main {
    public static void main(String[] args) {
        ObjectMapper mapper = new ObjectMapper();
        String orgJson = "{\"a\":{\"c\":{\"c_child\":\"2\"},\"d\":{\"d_child\":\"3\"}}}"; // JSON字符串
        String aimJson = "{\"b1\":{\"k2\":{\"k2_child\":\"v2_child\"},\"k3\":{\"k3_child\":\"v3_child\"}}}"; // JSON字符串
        List<JsonMapping> jsonMappings = new ArrayList<>();
        jsonMappings.add(new JsonMapping("root.b1.k2", "root.a.*", 4,new JsonMappingOptions(0,1,1,1)));
        JsonTranferUtil jsonTranferUtil = null;
        String result ="";
        try {
            jsonTranferUtil = new JsonTranferUtil(orgJson, aimJson, jsonMappings);
            result = jsonTranferUtil.tranJson();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("******************结果 **********************");
        System.out.println(result);
    }
}

执行结果如下:
在这里插入图片描述

3.多对多:将源结构的“a.*”值追加到目标结构的“b1.*”值

import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.List;
/**
 * 对象转换对象
 */
public class Main {
    public static void main(String[] args) {
        ObjectMapper mapper = new ObjectMapper();
        String orgJson = "{\"a\":{\"c\":{\"c_child\":\"2\"},\"d\":{\"d_child\":\"3\"}}}"; // JSON字符串
        String aimJson = "{\"b1\":{\"k2\":{\"k2_child\":\"v2_child\"},\"k3\":{\"k3_child\":\"v3_child\"}}}"; // JSON字符串
        List<JsonMapping> jsonMappings = new ArrayList<>();
        jsonMappings.add(new JsonMapping("root.b1.*", "root.a.*", 4,new JsonMappingOptions(0,1,1,1)));
        JsonTranferUtil jsonTranferUtil = null;
        String result ="";
        try {
            jsonTranferUtil = new JsonTranferUtil(orgJson, aimJson, jsonMappings);
            result = jsonTranferUtil.tranJson();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("******************结果 **********************");
        System.out.println(result);
    }
}

执行结果如下:
在这里插入图片描述

4.一对一:将源结构的“ a.* ”键追加到目标结构的“ b1.* ”值

import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.List;
/**
 * 对象转换对象
 */
public class Main {
    public static void main(String[] args) {
        ObjectMapper mapper = new ObjectMapper();
        String orgJson = "{\"a\":{\"c\":{\"c_child\":\"2\"},\"d\":{\"d_child\":\"3\"}}}"; // JSON字符串
        String aimJson = "{\"b1\":{\"k2\":{\"k2_child\":\"v2_child\"},\"k3\":{\"k3_child\":\"v3_child\"}}}"; // JSON字符串
        List<JsonMapping> jsonMappings = new ArrayList<>();
        jsonMappings.add(new JsonMapping("root.b1.*", "root.a.*", 4,new JsonMappingOptions(0,1,1,2)));
        JsonTranferUtil jsonTranferUtil = null;
        String result ="";
        try {
            jsonTranferUtil = new JsonTranferUtil(orgJson, aimJson, jsonMappings);
            result = jsonTranferUtil.tranJson();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("******************结果 **********************");
        System.out.println(result);
    }
}

执行结果如下:
在这里插入图片描述

五、在线转换工具

为了让使用者更加方便的配置出映射关系,为此开发了一套在线转换工具,可在工具中通过拖拽即可配置想要的结构转换关系,并可对转换关系所能实现的效果实时进行预览更改。

工具地址:数据转换工具

在这里插入图片描述

六、技术资料


网站公告

今日签到

点亮在社区的每一天
去签到