java解析数据库某列数组元素

本文介绍了一种使用Java从数据库中解析数组列的方法,通过遍历JSON数组并处理其中的元素,实现了对特定字段的读取和转换。该方法适用于处理复杂的数据库结构,如将订单ID和重复项等信息转换为特定格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

没找到用sql解析数组列的语句,最后用java实现。

代码和postman如下:

java代码:

public static void parseArray(String param) throws UnsupportedEncodingException {
        String url= params.get("body").replaceAll("%(?![0-9a-fA-F]{2})", "%25"); //转码(遇到(%)转译)
        param = URLDecoder.decode(url, "utf-8").trim();
        JSONObject json=JSONObject.fromObject(param);
        
        String dataName = json.getString("tableName");
        String where = json.getString("where");
        BatchService batchService = (BatchService)MyBeanFactory.getService("batchService");
        JSONObject DBJson=json.getJSONObject("DBJson");
        String field1="orderId";
        String field2="rep";
        List<JSONObject> findtPayment = batchService.findtPayment(dataName, where, DBJson);
        List<TDApplyVO> TDApplyVOLists=new ArrayList<>();
        for(JSONObject jParam:findtPayment){
            String jsonObject1= jParam.getString(field1);
            TDApplyVO tdApplyVO = new TDApplyVO();
            tdApplyVO.setOrderId(jsonObject1);
            if (jParam.get(field2)!=null) {
                final JSONArray jsonObject2= jParam.getJSONArray(field2);
                
                for (int i = 0; i < jsonObject2.size(); i++) {
                    JSONObject temp1=(JSONObject) jsonObject2.get(i);
                    String string=temp1.getString("item_id");
                    if (temp1.getString("item_id").equals("1253232")) {
                        JSONObject temp2=(JSONObject) temp1.get("item_detail");
                        tdApplyVO.setSevenApply(temp2.getString("platform_count"));
                    }else if (temp1.getString("item_id").equals("1253234")) {
                        JSONObject temp2=(JSONObject) temp1.get("item_detail");
                        tdApplyVO.setOneMouthApply(temp2.getString("platform_count"));
                    }else if(temp1.getString("item_id").equals("1253236")){
                        JSONObject temp2=(JSONObject) temp1.get("item_detail");
                        tdApplyVO.setThreeMouthApply(temp2.getString("platform_count"));
                    }
                }
            }
            TDApplyVOLists.add(tdApplyVO);
        }
        System.out.println(TDApplyVOLists.size());

        BufferedWriter writer;
        try {
            writer = new BufferedWriter(new FileWriter("./test/temp.txt"));
            for (TDApplyVO tdApplyVO : TDApplyVOLists) {
                writer.write(tdApplyVO.toString() + "\r\n");
            }
            writer.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

postman:

{"tableName":"temp0221","field":"","where":"","DBJson":{"url":"jdbc:mysql://127.0.0.1:3306/dataBase?characterEncoding=UTF-8" ,"username":"root","password":"root"}}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值