| | |
| | | private JSONArray covertToData(List<AssetImportLogDetailDto> assetImportLogDetailDtos) { |
| | | JSONArray datas = new JSONArray(); |
| | | |
| | | // 空列表直接返回空JSONArray |
| | | if (assetImportLogDetailDtos == null || assetImportLogDetailDtos.size() < 1) { |
| | | return datas; |
| | | } |
| | | |
| | | JSONObject data = null; |
| | | Object contentObj = null; |
| | | |
| | | for (AssetImportLogDetailDto assetImportLogDetailDto : assetImportLogDetailDtos) { |
| | | // 将DTO转换为JSONObject |
| | | data = BeanConvertUtil.beanCovertJson(assetImportLogDetailDto); |
| | | if (!StringUtil.isEmpty(assetImportLogDetailDto.getContent())) { |
| | | data.putAll(JSONObject.parseObject(assetImportLogDetailDto.getContent())); |
| | | contentObj = assetImportLogDetailDto.getContent(); |
| | | |
| | | // 处理content字段(真实值为String[],包含null、字符串、数字、布尔值) |
| | | if (contentObj != null && contentObj instanceof String[]) { |
| | | String[] contentArray = (String[]) contentObj; |
| | | // 转换String[]为JSONArray,保留原有数据类型(null、字符串等) |
| | | JSONArray contentJsonArray = new JSONArray(); |
| | | for (Object item : contentArray) { // 用Object接收,兼容数组内的null和各种类型 |
| | | contentJsonArray.add(item); |
| | | } |
| | | // 将数组存入JSONObject,key为"content" |
| | | data.put("content", contentJsonArray); |
| | | } else if (contentObj != null && contentObj instanceof String) { |
| | | // 兼容可能的字符串格式(保留原有逻辑) |
| | | String contentStr = (String) contentObj; |
| | | if (!StringUtil.isEmpty(contentStr)) { |
| | | try { |
| | | // 若字符串是JSON对象/数组,分别解析 |
| | | if (contentStr.startsWith("{")) { |
| | | data.putAll(JSONObject.parseObject(contentStr)); |
| | | } else if (contentStr.startsWith("[")) { |
| | | data.put("content", JSONArray.parseArray(contentStr)); |
| | | } else { |
| | | data.put("content", contentStr); |
| | | } |
| | | } catch (Exception e) { |
| | | // 非JSON格式字符串直接存入 |
| | | data.put("content", contentStr); |
| | | } |
| | | } |
| | | } |
| | | |
| | | datas.add(data); |
| | | } |
| | | |