From 436e4d807a7dd77b432a97e74146a2a9d7ccaa41 Mon Sep 17 00:00:00 2001 From: PL <774412461@qq.com> Date: Fri, 30 Aug 2024 16:45:39 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=BB=E5=8F=96json=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=E6=9B=B4=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/ChatMessageServiceImpl.java | 22 +++++++-- .../java/cn/teammodel/utils/FileUtil.java | 47 +++++++++++++++++++ 2 files changed, 64 insertions(+), 5 deletions(-) diff --git a/src/main/java/cn/teammodel/service/impl/ChatMessageServiceImpl.java b/src/main/java/cn/teammodel/service/impl/ChatMessageServiceImpl.java index ab66df3..aed57fa 100644 --- a/src/main/java/cn/teammodel/service/impl/ChatMessageServiceImpl.java +++ b/src/main/java/cn/teammodel/service/impl/ChatMessageServiceImpl.java @@ -23,6 +23,7 @@ import cn.teammodel.service.ChatMessageService; import cn.teammodel.utils.FileUtil; import cn.teammodel.utils.RepositoryUtil; import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.TypeReference; import com.azure.cosmos.models.CosmosPatchOperations; import lombok.extern.slf4j.Slf4j; @@ -313,13 +314,25 @@ public class ChatMessageServiceImpl implements ChatMessageService { //验证获取模型数据 异常问题 File file = null; try { - chatModels = readResourceFile("Json/ChatModel.json"); + String fileText = FileUtil.getFileText("Json/ChatModel.json"); + //转换问题 + try { + //String strData01 = JSON.toJSONString(fileText); + //获取聊天字段中的数据 + Object str = JSON.parseObject(fileText).get("chatModel"); + String strData1 = JSON.toJSONString(str); + //转换方式 + chatModels = JSON.parseObject(strData1, new TypeReference>() { }); + } catch (Exception e) { + throw new ServiceException(ErrorCode.OPERATION_ERROR.getCode(), "类型转换失败"); + } + + //chatModels = readResourceFile("Json/ChatModel.json"); //String str = readResourceFile("Json/ChatModel.json"); //file = new File(ClassLoader.getSystemResource("Json/ChatModel.json").getPath()); //相对路径获取文件信息 - //File file = new File("src/main/resources/Json/ChatModel.json"); //绝对路径获取文件信息 } catch (Exception e) { - throw new ServiceException(ErrorCode.OPERATION_ERROR.getCode(), "读取文件" + Arrays.toString(e.getStackTrace())); + throw new ServiceException(ErrorCode.OPERATION_ERROR.getCode(), "读取文件" + Arrays.toString(e.getStackTrace()) + e.getMessage()); } /*try { //chatModel = readerMethod(file); @@ -511,8 +524,7 @@ public class ChatMessageServiceImpl implements ChatMessageService { //转换问题 try { //转换方式 - chatModelDtos = JSON.parseObject(strData2, new TypeReference>() { - }); + chatModelDtos = JSON.parseObject(strData2, new TypeReference>() {}); } catch (Exception e) { throw new ServiceException(ErrorCode.OPERATION_ERROR.getCode(), "类型转换失败"); } diff --git a/src/main/java/cn/teammodel/utils/FileUtil.java b/src/main/java/cn/teammodel/utils/FileUtil.java index 0174172..383c172 100644 --- a/src/main/java/cn/teammodel/utils/FileUtil.java +++ b/src/main/java/cn/teammodel/utils/FileUtil.java @@ -7,6 +7,12 @@ import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import com.alibaba.fastjson.JSONObject; +import org.apache.commons.lang3.StringUtils; +import org.springframework.core.io.ClassPathResource; +import java.io.InputStreamReader; +import java.io.Reader; + public class FileUtil { /** @@ -32,4 +38,45 @@ public class FileUtil { } } + /** + * 根据key获取json + * @param filepath 文件路径 + * @param key json的key + * @return + */ + public static Object getJSONByKey(String filepath, String key){ + String text = FileUtil.getFileText(filepath); + JSONObject object = new JSONObject(); + if (StringUtils.isNoneBlank(text)){ + object = parseObject(text); + } + return object.get(key); + } + + /** + * 将json 字符串转换为json对象 + */ + public static JSONObject parseObject(String text) { + return JSONObject.parseObject(text); + } + + /** + * 获取文件内文本 + * @param filepath + * @return + */ + public static String getFileText(String filepath){ + StringBuffer sb = new StringBuffer(); + try{ + ClassPathResource classPathResource = new ClassPathResource(filepath); + Reader reader = new InputStreamReader(classPathResource.getInputStream()); + int ch = 0; + while ((ch = reader.read())!=-1){ + sb.append((char)ch); + } + }catch (Exception e) { + e.printStackTrace(); + } + return sb.toString().replaceAll("\r\n","").replaceAll(" ",""); + } }