将表1JOSN字符串分割存入另一个表、添加两表连接后的某几列

这篇博客介绍了如何将包含JSON数组的字符串转换为JSONArray对象,并遍历其内容,然后将数据保存到数据库中。文章通过Java代码展示了如何使用json-lib库进行操作,包括数据获取、转换和保存的过程。
摘要由CSDN通过智能技术生成

Resouce:

@RestController
@Api(tags = "软件信息")
@DustMapping(value = "/soft", method = RequestMethod.POST)
public class SoftResource {

    @Autowired
    SoftService softService;

    @ApiOperation(value = "softInfo", notes = "软件信息",
            consumes = "application/json", produces = "application/json")
   /* @ApiImplicitParams({
            @ApiImplicitParam(name = "body", value = "{id:编号}", required = true, dataType = "String", paramType = "body")
    })*/
    @ApiResponses({
            @ApiResponse(code = 200,
                    message = "| 错误码   | 释义  |\n" +
                            "| - | - |\n" +
                            "| SUCCESS  | 成功 |\n" +
                            "| DB_ERR | 数据库异常 |\n")
    })
    @DustMapping(value = "/")
/*    public Object softInfo(@RequestBody JSONObject body) throws SQLException, DustMsException, BuzException {
  //   int id = body.getInteger("id");
     SoftService s =new SoftService();
     return s.info(body);

    }*/
    public Object softInfo() throws SQLException, DustMsException, BuzException{

        return softService.info();
    }

}

Service:

@Service
public class SoftService extends BaseService {
    public Object info() throws SQLException, BuzException, DustMsException {

        DataObj obj = DataObjBuilder.create(VERSION.APP_ID, "eqt", "eqt_soft_all");
        obj.search();
        ISqlAdapter adapter = getAdapter();
        int os = obj.getRows().size();
        DataObj obj2 = DataObjBuilder.create(VERSION.APP_ID, "eqt", "soft_info");
        for(int j = 0;j < os;j++) {

            DataObjRow row = obj.getRow(j);
            System.out.println(j);
            String info = (String) obj.getValue(row, "softInfo");

            String uuid = (String) obj.getValue(row, "uuid");
            String customerId = (String) obj.getValue(row, "customerId");
            Date gmtGetDate = (Date) obj.getValue(row, "gmtGetDate");

            String[] sinfo = info.split("\\{");
            for (int i = 0; i < sinfo.length; i++) {
                int softProdIndex = (sinfo[i]).indexOf("\"softProd\":\"");

                if (softProdIndex == -1) {
                    continue;
                }
               String js = "{"+(String)sinfo[i];
                js = js.substring(0,js.length()-1);
                JSONObject jsonObject = JSONObject.parseObject(js);

                    DataObjRow nrow = obj2.newRow();
                    nrow.setValue("softProd", jsonObject.getString("softProd"));
                    nrow.setValue("softVersion", jsonObject.getString("softVersion"));
                    nrow.setValue("installDate", jsonObject.getString("installDate"));
                    nrow.setValue("softName", jsonObject.getString("softName"));
                    nrow.setValue("softPath", jsonObject.getString("softPath"));
                    nrow.setValue("uuid", uuid);
                    nrow.setValue("customerId", customerId);
                    nrow.setValue("gmtGetDate", gmtGetDate);
                    obj2.addRow(nrow);
                    obj2.validate();
                    //obj2.save();
                    // obj2.search();
                    if (obj2.getRows().size() == 0) {
                        throw new BuzException("添加失败");
                    }
            }
            obj2.save(adapter);
        }
        adapter.commit();
        adapter.close();
        return obj;
    }
}

ps:注意JOSN字符串格式

因info信息是JSON数组,修改如下

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import com.hisense.hitms.VERSION;

import dust.db.dict.*;

import dust.db.sql.ISqlAdapter;
import dust.service.micro.common.BuzException;
import dust.service.micro.common.DustMsException;
import org.springframework.stereotype.Service;
import java.sql.SQLException;

/**
 * Created by Administrator on 2021/10/9.
 */
@Service
public class SoftService extends BaseService {
    public Object info() throws SQLException, BuzException, DustMsException {

        DataObj obj = DataObjBuilder.create(VERSION.APP_ID, "eqt", "soft_info");
        DataObj obj2 = DataObjBuilder.create(VERSION.APP_ID, "eqt", "eqt_main_info123");

        obj2.search();
        ISqlAdapter adapter = getAdapter();

        int os = obj2.getRows().size();
        for (int j = 0; j < os; j++) {   //遍历eqt_main_info123行
            DataObjRow row = obj2.getRow(j);
            System.out.println(j);
            String uuid = (String) obj2.getValue(row, "uuid");
            String info = (String) obj2.getValue(row, "softInfo");
            if(info==null){
                continue;
            }
            String customerId = (String) obj2.getValue(row, "customerId");
            String gmtGetDate = (String) obj2.getValue(row, "gmtGetDate");
            String eqtName = (String) obj2.getValue(row, "eqtName");
            String eqtCode = (String) obj2.getValue(row, "eqtCode");
            Long orgId = (Long) obj2.getValue(row, "orgId");
            JSONArray json = JSONArray.fromObject(info);
            if(json.size()>0){
                for (int i = 0; i < json.size(); i++) {   //遍历sinfo

                    JSONObject jsonObject = json.getJSONObject(i);

                    DataObjRow nrow = obj.newRow();
                    nrow.setValue("softProd", jsonObject.getString("softProd"));
                    nrow.setValue("softVersion",jsonObject.getString("softVersion"));
                    nrow.setValue("installDate",jsonObject.getString("installDate"));
                    nrow.setValue("softName", jsonObject.getString("softName"));
                    nrow.setValue("softPath", jsonObject.getString("softPath"));
                    nrow.setValue("uuid", uuid);
                    nrow.setValue("orgId", orgId);
                    nrow.setValue("customerId", customerId);
                    nrow.setValue("gmtGetDate", gmtGetDate);
                    nrow.setValue("eqtName", eqtName);
                    nrow.setValue("eqtCode", eqtCode);

                    obj.addRow(nrow);
                    obj.validate();

                    if (obj.getRows().size() == 0) {
                        throw new BuzException("添加失败");
                    }
                }
            }

            obj.save(adapter);
            adapter.commit();
        }
        return obj2;
    }

}

参考:java 字符串转成 json 数组并且遍历 - 逐梦寻欢 - 博客园

当需要把一串字符串转成一个json 数组 ,并遍历其中的内容时。

首先要导入 net.sf.json.JSONArray和net.sf.json.JSONObject 两个jar 包

1

2

3

4

5

6

<dependency>

   <groupId>net.sf.json-lib</groupId>

   <artifactId>json-lib</artifactId>

   <version>2.4</version>

   <classifier>jdk15</classifier>

</dependency>

  

1

2

3

4

5

6

7

8

9

10

String str = "[{name:'a',value:'aa'},{name:'b',value:'bb'},{name:'c',value:'cc'},{name:'d',value:'dd'}]" ;  // 一个未转化的字符串

JSONArray json = JSONArray.fromObject(str ); // 首先把字符串转成 JSONArray  对象

if(json.size()>0){

  for(int i=0;i<json.size();i++){

    JSONObject job = json.getJSONObject(i);  // 遍历 jsonarray 数组,把每一个对象转成 json 对象

    System.out.println(job.get("name")+"=") ;  // 得到 每个对象中的属性值

  }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值