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 |
|
1 2 3 4 5 6 7 8 9 10 |
|