java读取文件多个json对象快速转为实体类

个人小工具使用:
苦于老和其他现场对接,数据提取不方便,排查问题不好排查,只能将现场数据导回来,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;
    }  
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值