拧螺丝需求:递归算法的极致应用

news/2024/10/20 16:36:54/

前言

在一个平平无奇的下午,接到一个需求,需要给公司的中台系统做一个json报文重组的功能。
因为公司的某些业务需要外部数据的支持,所以会采购一些其它公司的数据,而且为了保证业务的连续性,同一种数据会采购多方的数据源
在这里插入图片描述
这里就出现了一个问题:

1.每个数据源的返回报文并不是一样的,所以需要在中台去进行转换,将不同数据源返回数据的结构重新组合
在这里插入图片描述

1.需求分析

首先,返回数据的格式为JSON,需要对原始数据的结构进行重组,也就是将原报文的值,放到新报文中,我将这个功能点拆成了两点:

① 取出原报文的值

② 构建新报文并将原报文的值放进去

根据上面这两点,我一下就想到使用递归算法来完成这个功能点

我们来看看递归算法是什么

递归算法

1.递归的基本思想:将一个大问题分解为若干个小问题,每个小问题都和原问题类似,但规模更小,直到最终问题可以轻易地被解决。

2.递归的终止条件:递归算法必须有一个明确的终止条件,否则算法将无限递归下去,导致程序崩溃。

3.递归的调用方式:在递归算法中,每次递归调用都会将问题的规模缩小,并将结果传递给下一次递归调用,直到最终得到问题的解。

4.递归的优势和劣势:递归算法可以使代码更加简洁清晰,同时也能够更自然地表达数学和物理模型等自然现象。但是,递归算法也存在一些劣势,比如可能会出现栈溢出等问题,同时递归算法的性能通常也不如迭代算法。

2.取出原报文的值

配置信息

首先,我们使用 . 来区分层级

例如:
在这里插入图片描述
这里我们注意三点:

1.判断原报文的节点为JSONArray还是JSONObject

2.是否为最后一层

3.要有明确中断条件,这里我们中断条件就是sourceKey不包含. 也就是挖掘报文到达了配置的最后一层

private static JSONObject getSourceJSON(Object obj, String sourceKey) {JSONObject result = new JSONObject();//如果最后一层则没有.if (!sourceKey.contains(".")) {result.put("keyName", sourceKey);result.put("data", obj);} else {//否则就不断向下递归//取出配置项中的第一节点,每一次递归,我们的第一节点也在这一层被消耗String tmpKey = sourceKey.substring(0, sourceKey.indexOf(" ."));String sourceKeyAfter = sourceKey.substring(sourceKey.indexOf(". ") + 1, sourceKey.length());String keyName = sourceKey.substring(sourceKey.lastIndexOf(".") + 1, sourceKey.length());//判断报文的数据类型if (obj instanceof JSONArray) {JSONArray ja = (JSONArray) obj;JSONArray tmpJA = new JSONArray();for (int i = 0; 1 < ja.size(); i++) {JSONObject jo = ja.getJSONObject(i);//不断向下挖掘JSONObject tmpJO = getSourceJSON(jo.get(tmpKey), sourceKeyAfter);Object o = tmpJO.get("data");if (o instanceof JSONArray) {JSONArray array = (JSONArray) o;for (int j = 0; j < array.size(); j++) {tmpJA.add(array.get(j));}} else {tmpJA.add(o);}}result.put("keyName", keyName);result.put("data", tmpJA);} else if (obj instanceof JSONObject) {JSONObject jo = (JSONObject) obj;return getSourceJSON(jo.get(tmpKey), sourceKeyAfter);}}return result;}

最后返回的是一个JSONObject,里面有两个key

① keyName:节点名称

② data:源数据

目的就是通过keyName能保证能从data直接取出数据

验证例子:

我们直接拿上面的JSON报文来进行演示,获取报文中url片段

String source = "{\"sites\":{\"site\":[{\"id\":\"1\",\"name\":\"baidu\",\"url\":\"www.baidu.com\"},{\"id\":\"2\",\"name\":\"Java\",\"url\":\"www.java.com\"},{\"id\":\"3\",\"name\":\"Google\",\"url\":\"www.google.com\"}]}}";
JSONObject sourceJSON = JSONObject.parseObject(source);
//直接打印结果
System.out.println(getSourceJSON(sourceJSON, "sites.site.url"));

输出:
在这里插入图片描述
通过keyName,判断data中的数据格式,JSONObject直接获取,JSONArray循环获取,这样我们就可以拿到url的值了

构建新报文并赋值

这也是一层层的构建,最后返回一整个重构后的报文,依旧使用我们的递归算法

由于是一层层,所以我们针对每一层,都需要配置它的数据类型

先给出配置
在这里插入图片描述
我们将此配置放到Map里,结构如下

JSONObject jo1 = new JSONObject(),jo2 = new JSONObject(),jo3 = new JSONObject(),jo4 = new JSONObject(),jo5 = new JSONObject();
jo1.put("type", "array");
jo1.put("sourceKey", "");
map.put("data", jo1);jo2.put("type", "array");
jo2.put("sourceKey", "");
map.put("data.baseInfo", jo2);jo3.put("type", "array");
jo3.put("sourceKey", "sites.site.id");
map.put("data.baseInfo.newid", jo3);jo4.put("type", "array");
jo4.put("sourceKey", "sites.site.name");
map.put("data.baseInfo.newname", jo4);jo5.put("type", "array");
jo5.put("sourceKey", "sites.site.url");
map.put("data.baseInfo.newurl", jo5);

map的key为目标构建路径,value为JSONObject,里面包含 ① 数据类型 ② 源路径

构建新报文的代码,需要注意以下几点

① 只有在最后一层才进行构建报文+赋值,前面全为构建

② 在构建之前判断节点是否存在,存在直接取出来进行递归,不存在再根据类型创建新节点

==③ 要有明确中断条件,这里我们中断条件就是targetKey不包含. ==

public static Object rebuildJSON(String targetKey, Object obj, Map<String, JSONObject> map, Object sourceObj,String sourceKey, String oldKey) {Object result = null;if (targetKey.contains(".")) {String tmpkey = targetKey.substring(0, targetKey.indexOf("."));String tmpkeyType = "array";if ("".equals(oldKey)) {tmpkeyType = map.get(tmpkey).getString("type");} else {tmpkeyType = map.get(oldKey + "." + tmpkey).getString("type");}String afterkey = targetKey.substring(targetKey.indexOf(".") + 1, targetKey.length());if (obj instanceof JSONArray) {JSONArray ja = (JSONArray) obj;JSONArray newJA = new JSONArray();if (ja.size() > 0) {for (int i = 0; i < ja.size(); i++) {JSONObject jo = ja.getJSONObject(i);Object tmpO = jo.get(tmpkey);if (tmpO == null) {if ("array".equals(tmpkeyType)) {JSONArray tmpja = new JSONArray();jo.put(tmpkey, tmpja);} else if ("object".equals(tmpkeyType)) {JSONObject tmpjo = new JSONObject();jo.put(tmpkey, tmpjo);}}tmpO = rebuildJSON(afterkey, jo.get(tmpkey), map, sourceObj, sourceKey, tmpkey);jo.put(tmpkey, tmpO);newJA.add(jo);}} else {JSONObject jo = new JSONObject();if ("array".equals(tmpkeyType)) {JSONArray tmpja = new JSONArray();jo.put(tmpkey, tmpja);} else if ("object".equals(tmpkeyType)) {JSONObject tmpjo = new JSONObject();jo.put(tmpkey, tmpjo);}Object tmpO = rebuildJSON(afterkey, jo.get(tmpkey), map, sourceObj, sourceKey, tmpkey);jo.put(tmpkey, tmpO);newJA.add(jo);}result = newJA;} else if (obj instanceof JSONObject) {JSONObject jo = (JSONObject) obj;JSONObject newJo = jo;Object tmpO = jo.get(tmpkey);if (tmpO == null) {if ("array".equals(tmpkeyType)) {JSONArray tmpja = new JSONArray();jo.put(tmpkey, tmpja);} else if ("object".equals(tmpkeyType)) {JSONObject tmpjo = new JSONObject();jo.put(tmpkey, tmpjo);}}tmpO = rebuildJSON(afterkey, jo.get(tmpkey), map, sourceObj, sourceKey, tmpkey);newJo.put(tmpkey, tmpO);result = newJo;}} else {if (obj instanceof JSONArray) {JSONArray ja = (JSONArray) obj;JSONArray sourceja = (JSONArray) sourceObj;JSONArray newJA = new JSONArray();if (sourceja.size() > ja.size()) {for (int i = 0; i < sourceja.size(); i++) {JSONObject targetJO;if (ja.size() > i) {targetJO = ja.getJSONObject(i);} else {targetJO = new JSONObject();}JSONObject sourceJO = sourceja.getJSONObject(i);targetJO.put(targetKey, sourceJO.get(sourceKey));newJA.add(targetJO);}} else {for (int i = 0; i < ja.size(); i++) {if (sourceja.size() > i) {JSONObject targetJO = ja.getJSONObject(i);JSONObject sourceJO = sourceja.getJSONObject(i);targetJO.put(targetKey, sourceJO.get(sourceKey));newJA.add(targetJO);}}}result = newJA;} else {JSONObject jo = (JSONObject) obj;JSONObject sourcejo = (JSONObject) sourceObj;jo.put(targetKey, sourcejo.get(sourceKey));result = jo;}}return result;}

测试代码:

public static void main(String[] args) {String source = "{\"sites\":{\"site\":[{\"id\":\"1\",\"name\":\"baidu\",\"url\":\"www.baidu.com\"},{\"id\":\"2\",\"name\":\"Java\",\"url\":\"www.java.com\"},{\"id\":\"3\",\"name\":\"Google\",\"url\":\"www.google.com\"}]}}";JSONObject sourceJSON = JSONObject.parseObject(source);Map<String,JSONObject> map = new HashMap<>();JSONObject jo1 = new JSONObject(),jo2 = new JSONObject(),jo3 = new JSONObject(),jo4 = new JSONObject(),jo5 = new JSONObject();jo1.put("type", "array");jo1.put("sourceKey", "");map.put("data", jo1);jo2.put("type", "array");jo2.put("sourceKey", "");map.put("data.baseInfo", jo2);jo3.put("type", "object");jo3.put("sourceKey", "sites.site.id");map.put("data.baseInfo.newid", jo3);jo4.put("type", "object");jo4.put("sourceKey", "sites.site.name");map.put("data.baseInfo.newname", jo4);jo5.put("type", "object");jo5.put("sourceKey", "sites.site.url");map.put("data.baseInfo.newurl", jo5);Object result = new JSONObject();for(Map.Entry<String,JSONObject> entry : map.entrySet()) {String sourceKey = entry.getValue().getString("sourceKey");//源路径为空我们不获取它的原值if("".equals(sourceKey)) {continue;}JSONObject sourceObj = getSourceJSON(sourceJSON, sourceKey);result = rebuildJSON(entry.getKey(), result, map, sourceObj.get("data"), sourceObj.getString("keyName"), "");}System.out.println(result);}

测试输出结果:
在这里插入图片描述
到这一步螺丝终于拧完了!

这个只能针对标准的JSON报文去处理,在这一步前我还利用了其它的算法将不标准的报文转化为标准的,详细代码关注我后续会讲解

在这里插入图片描述


http://www.ppmy.cn/news/665366.html

相关文章

游戏平台 在线无限畅玩 支持 手机电脑 多端模式 官服 BT 满V GM版本

http://h5.zyttx.com 游戏平台 在线无限畅玩 支持 手机电脑 多端模式 官服 BT 满V GM版本 http://1.gmsyb.com 官服 BT 满V GM版本手游招CPS代理 50%分成 日结 自动开代理后台 群956018737只需要分享给玩家立即享受收益 h5游戏放CPS合作&#xff0c;寻平台公众号联运合作&#…

一个游戏大量合服代表什么_网游合服对于这个网游意味着什么?

展开全部 合服意味着这两个服务区人数不多啊62616964757a686964616fe58685e5aeb931333365643662&#xff0c;这个应该谁多知道啊&#xff01;当一个老区开了太久了玩家流失严重&#xff0c;让继续在老区玩的玩家体验很差&#xff0c;所以选择合区来增加一个区的玩家数量&#x…

【H5网游服务端】决战魔域H5一键即玩服务端+授权GM后台+外网教程

下载链接&#xff1a;https://pan.baidu.com/s/1ds_xFq1Rd1_xC4515BRGXw 提取码&#xff1a;soho 【决战魔域H5】一键即玩服务端授权GM后台外网教程

手机网游研究之一

不知道是MS推出了新技术对我的引导还是自己一直认为自己要有所人生的突破的源由,想开发一 套ERP系统,以后没有钱了好坐在家里卖(~_~),想开发一些基于条形码系列的软件,因为最近两年都是在修 改与条码枪与PLC有关的自动化控制方向的软件系统,但感觉这方便技术已经比较成熟且新…

网游客户端

今天去面试了两个职位&#xff0c;第一个是网游客户端程序员&#xff0c;第二个是android/ios游戏程序员。下面我来说说我今天的感悟吧~~~ 1.应聘网游客户端程序员&#xff0c; 我本来不是太想去的&#xff0c;我做pc网游我做不来&#xff0c;其实就是不喜欢&#xff0c;但是没…

页游与端游合体 微端网游或成为行业风向标

近些年&#xff0c;网页游戏异军突起让传统端游遭到了一定程度的冲击&#xff0c;但经过一段时间的过渡期&#xff0c;页游在发展过程中也遭遇瓶颈面临重重困境&#xff0c;粗糙的游戏品质成为其继续发展的致命死穴。如何在竞争激烈的市场杀出一片蓝海&#xff0c;是很多厂商们…

C#网游客户端

C#网游客户端 一、项目创建以及页面设置&#xff08;一&#xff09;新建项目&#xff08;二&#xff09;界面设计 二、连接服务器三、客户端向服务器发送数据四、播放背景音乐五、切换游戏背景图片六、总结七、参考资料 一、项目创建以及页面设置 &#xff08;一&#xff09;新…

编写网游客户端

编写网游客户端 一、目的二、项目完成1. 新建项目2.界面3.客户端发送数据4.播放背景音乐5.实现游戏背景图片变换 三、总结四、参考 一、目的 编写一个网游客户端&#xff0c;游戏服务器的校园内网IP地址为 10.1.230.41&#xff0c;端口为3900&#xff0c;采用TCP连接。 1.连接…