JSONObject jsonObject = JSON.parseObject(value);、
这是一行Java代码,它的作用是将一个字符串转换为JSONObject对象。其中,value是一个字符串,它包含了JSON格式的数据。JSON是一种轻量级的数据交换格式,常用于前后端数据传输。JSON.parseObject()方法是阿里巴巴的fastjson库提供的方法,它可以将JSON格式的字符串转换为Java对象。
举个例子,如果value是以下JSON格式的字符串:
{
"name": "Tom",
"age": 18,
"gender": "male"
}
那么执行以下代码:
JSONObject jsonObject = JSON.parseObject(value);
就会得到一个JSONObject对象,它包含了三个属性:name、age和gender,它们的值分别是"Tom"、18和"male"。
优化如下代码:public CommonResult paymentRequest( @ApiParam(name = "params", value = "表单bo") @RequestBody String params) throws Exception { if (org.apache.commons.lang.StringUtils.isBlank(params)) { return new CommonResult(false, "传入参数错误", null); } log.info("-----------"+params); JSONObject json1 = com.alibaba.fastjson.JSON.parseObject(params); // JSONObject bo = com.alibaba.fastjson.JSON.parseObject(json1.getString("boData")); // Map<String,String> map = JSONObject.parseObject(bo.getString("lxcgfksq2"),Map.class); // JSONObject creator = com.alibaba.fastjson.JSON.parseObject(json1.getString("creator")); // 流程申请人 String instId = json1.getString("instId"); // 实例id String flowKey = json1.getString("flowKey"); // 流程实例类型 if ("lxcgfksq".equals(flowKey)){ flowKey = "lxcg"; } else if ("shddfksq".equals(flowKey)){ flowKey = "shdd"; } else if ("ylshhtglysfksq".equals(flowKey)){ flowKey = "htgl"; } String actionName = json1.getString("actionName"); // 审批结果 String sysCode = json1.getString("sysCode"); if (sysCode == null) { BpmResultSapProcess bpmResultSapProcess = new BpmResultSapProcess(); bpmResultSapProcess.setInstId(instId); bpmResultSapProcess.setInstType(flowKey); bpmResultSapProcess.setJsonstr(params); bpmResultSapProcess.setApproveStatus(actionName); bpmResultSapProcess.setCreateTime(new Date()); bpmResultSapProcess.setCreateUser("admin"); piecemealPurchasesManager.create(bpmResultSapProcess); } return new CommonResult(true, "写入成功", null); }
public CommonResult paymentRequest(@ApiParam(name = "params", value = "表单bo") @RequestBody String params) throws Exception { if (StringUtils.isBlank(params)) { return new CommonResult(false, "传入参数错误", null); } log.info("-----------" + params); JSONObject json1 = JSON.parseObject(params); String flowKey = json1.getString("flowKey"); // 流程实例类型 if ("lxcgfksq".equals(flowKey)) { flowKey = "lxcg"; } else if ("shddfksq".equals(flowKey)) { flowKey = "shdd"; } else if ("ylshhtglysfksq".equals(flowKey)) { flowKey = "htgl"; } String sysCode = json1.getString("sysCode"); if (sysCode == null) { BpmResultSapProcess bpmResultSapProcess = new BpmResultSapProcess(); bpmResultSapProcess.setInstId(json1.getString("instId")); bpmResultSapProcess.setInstType(flowKey); bpmResultSapProcess.setJsonstr(params); bpmResultSapProcess.setApproveStatus(json1.getString("actionName")); bpmResultSapProcess.setCreateTime(new Date()); bpmResultSapProcess.setCreateUser("admin"); piecemealPurchasesManager.create(bpmResultSapProcess); } return new CommonResult(true, "写入成功", null); }
优化点:
- 引入静态导入,避免频繁写出类名;
- 删除注释代码,减少代码量和维护成本;
- 删除不必要的变量声明,直接使用 JSONObject 获取信息;
- 简化 if-else 判断流程类型,使用字符串替换;
- 简化对象属性赋值,直接使用 JSONObject 获取信息。
SingleOutputStreamOperator<List<JSONObject>> applyDataStream = dataStreamSource.keyBy(value -> { String[] splitValues = value.getString("key").toString().split(":"); return splitValues[2]; }).window(TumblingProcessingTimeWindows.of(Time.seconds(2))).apply(new WindowFunction<JSONObject, List<JSONObject>, String, TimeWindow>() { @Override public void apply(String key, TimeWindow timeWindow, Iterable<JSONObject> input, Collector<List<JSONObject>> out) throws Exception { List<JSONObject> output = new ArrayList<>(); input.forEach(s -> { // String topicName = ModelUtil.getConfigValue("ods_kafka_all_data_topicName") + "_" + key; //主题名称 String value = s.getString("value"); TranslateJson translateJson = JSON.parseObject(value, TranslateJson.class); if ("read".equals(translateJson.getType()) || "c".equals(translateJson.getType())) {//新增的数据才同步 使用这种写法优化上面代码
您可以使用 Lambda 表达式来优化上面的代码,示例代码如下:
SingleOutputStreamOperator<List<JSONObject>> applyDataStream = dataStreamSource
.keyBy(value -> value.getString("key").toString().split(":")[2])
.window(TumblingProcessingTimeWindows.of(Time.seconds(2)))
.apply((String key, TimeWindow timeWindow, Iterable<JSONObject> input, Collector<List<JSONObject>> out) -> {
List<JSONObject> output = new ArrayList<>();
input.forEach(s -> {
String value = s.getString("value");
TranslateJson translateJson = JSON.parseObject(value, TranslateJson.class);
if ("read".equals(translateJson.getType()) || "c".equals(translateJson.getType())) {
// 新增的数据才同步
// String topicName = ModelUtil.getConfigValue("ods_kafka_all_data_topicName") + "_" + key; //主题名称
output.add(s);
}
});
out.collect(output);
});
使用 Lambda 表达式可以使代码更加简洁易读,同时也可以提高代码的执行效率。
相关推荐
















