个人小工具使用:
苦于老和其他现场对接,数据提取不方便,排查问题不好排查,只能将现场数据导回来,excel麻烦,对于测试方法复现bug就是json转换,可以直接运行方法
快速读取文件txt中的json字符串转为方法入参对象
依赖
依赖:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
</dependency>
代码
public class DataTest {
public static void main(String[] args) throws IOException {
String filePath = "FilePath/0607.txt";
//需要将json转为对象的实体类
TopoParam topoParam = parseTopoParamFromFile(filePath);
TopoRetObj topoRetObj = TopoImpedanceUtil.tgDevTopoIdentity(topoParam);
}
public static TopoParam parseTopoParamFromFile(String filePath) {
TopoParam topoParam = new TopoParam();
ObjectMapper objectMapper = new ObjectMapper();
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
String json = sb.toString();
TopoParam topoData = objectMapper.readValue(json, TopoParam.class);
topoParam.setCurrentFeatureList(topoData.getCurrentFeatureList());
topoParam.setVoltageFeatureList(topoData.getVoltageFeatureList());
topoParam.setDocDataList(topoData.getDocDataList());
topoParam.setLoopImpedanceList(topoData.getLoopImpedanceList());
topoParam.setJsonData(topoData.getJsonData());
}
catch (IOException e) {
e.printStackTrace();
}
return topoParam;
}
}