0
点赞
收藏
分享

微信扫一扫

解析json数据导出某个字段的值到excel(记录)


记录一下导出代码

public static void main(String[] args) {
		// 将你的json数据放在txt文档里并复制路径到下面
        String filePath = "E:/home/2023年需求/人才/20230801出表/job_mobile_json.txt";  // 替换成你的文件路径

        try {
            String content = readFileAsString(filePath);
//            System.out.println(content);
            int emptyTotal = 0;
            int notNullTotal = 0;
            for (Object object : JSON.parseArray(content)) {
                JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(object));
                JSONObject source = jsonObject.getJSONObject("_source");
                if (Objects.nonNull(source) && !source.toString().equals("{}")) {
                    JSONObject jobUser = source.getJSONObject("jobUser");
                    String mobile = jobUser.getString("mobile");
                    if(StringUtils.isNotEmpty(mobile)){
                        System.out.println(mobile);
                        notNullTotal ++;
                    }else{
                        emptyTotal++;
                    }
                }else{
                    emptyTotal++;
                }

            }
            System.out.println("不为空:"+notNullTotal);
            System.out.println("为空:"+emptyTotal);


        } catch (IOException e) {
            System.err.println("Failed to read file: " + e.getMessage());
        }
    }

    public static String readFileAsString(String filePath) throws IOException {
        Path path = Paths.get(filePath);
        byte[] bytes = Files.readAllBytes(path);
        return new String(bytes);
    }


举报

相关推荐

0 条评论