读取json文件方式更换

develop
PL 4 months ago
parent 54bf566482
commit 436e4d807a

@ -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<List<ChatModelDto>>() { });
} 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<List<ChatModelDto>>() {
});
chatModelDtos = JSON.parseObject(strData2, new TypeReference<List<ChatModelDto>>() {});
} catch (Exception e) {
throw new ServiceException(ErrorCode.OPERATION_ERROR.getCode(), "类型转换失败");
}

@ -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 {
}
}
/**
* keyjson
* @param filepath
* @param key jsonkey
* @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(" ","");
}
}

Loading…
Cancel
Save