一聚教程网:一个值得你收藏的教程网站

热门教程

java读取文件内容解析Json格式数据方式代码示例

时间:2022-06-29 02:12:34 编辑:袖梨 来源:一聚教程网

本篇文章小编给大家分享一下java读取文件内容解析Json格式数据方式代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

一、读取txt文件内容(Json格式数据)

    public static String reader(String filePath) {
        try {
            File file = new File(filePath);
            if (file.isFile() && file.exists()) {
                InputStreamReader read = new InputStreamReader(new FileInputStream(file), "UTF-8");
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = bufferedReader.readLine();
                while (lineTxt != null) {
                    return lineTxt;
                }
            }
        } catch (UnsupportedEncodingException | FileNotFoundException e) {
            System.out.println("Cannot find the file specified!");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("Error reading file content!");
            e.printStackTrace();
        }
        return null;
    }

二、解析处理Json格式数据

    private static void process(String txtStr) {
        JSONObject json = JSONObject.fromObject(txtStr);
        JSONArray datas = json.getJSONObject("data").getJSONArray("rows");
        List> list = new ArrayList<>();
        for (int i = 0; i < datas.length(); i++) {
            Map map = new HashMap<>();
            JSONObject obj = datas.getJSONObject(i).getJSONObject("cells");
            String name = obj.getString("weibo_name");
            String code = obj.getString("weibo_id");
            String url = BASE_URL + obj.getString("url");
            map.put("name", name);
            map.put("code", code);
            map.put("url", url);
            list.add(map);
        }
        if (!list.isEmpty()) {
            insert(list);
        }
    }

三、结果存入数据库

    private static void insert(List> list) {
        for (Map map : list) {
            //遍历数据,写存储方法
        }
    }

四、测试

    public static void main(String[] args) {
        String filePath = "E:wugangdataweiboyiwechat.txt";
        String txtStr = reader(filePath);
        if (txtStr != null) {
            process(txtStr);
        } else {
            System.out.println("Read the content is empty!");
        }
        System.out.println("--- end ---");
    }

java 读取txt文件中的json数据,进行导出

txt文件中的内容如下

以下代码可直接运行

package com.hwt.count.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;
import net.sf.json.JSONObject;
public class Testaa {
    public static void main(String[] args) {
        try {
            String path = "C:/Users/dell/Desktop/test.txt";
            File file = new File(path);
            InputStreamReader isr = new InputStreamReader(new FileInputStream(file),"GBK");
            BufferedReader br = new BufferedReader(isr);
            String content = br.readLine() ;
            br.close();
            isr.close();
            content = content.substring(2, content.length()-2);
            content = content.replace("},{", ";");
            String[] arrContent = content.split(";");
            
            //设置列头名称和表体数据
            String[] rowsName = new String[]{"code_type","code","name"};
            List dataList = new ArrayList();
            
            for(String arrc : arrContent){
                JSONObject jsonObj = JSONObject.fromObject("{"+arrc+"}");
                String code = jsonObj.getString("code");
                String name = jsonObj.getString("name");
                Object[] obj = new Object[rowsName.length];
                obj[0] = "TYPE";
                obj[1] = code;
                obj[2] = name;
                dataList.add(obj);
            }
            //设置列头名称和表体数据
            HSSFWorkbook workbook = setWorkBookDate(dataList,rowsName);
            try {
                // 将workbook对象输出到文件test.xls
                FileOutputStream fos = new FileOutputStream("C:/Users/dell/Desktop/test.xls");
                workbook.write(fos);
                fos.flush(); // 缓冲
                fos.close(); // 关闭流
            }catch (Exception e1) {
                e1.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private static HSSFWorkbook setWorkBookDate(List  dataList,String[] rowsName){
        
        //创建工作簿对象
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建工作表,设置当前页名称
        HSSFSheet sheet = workbook.createSheet("测试");
        //设置默认行高
        sheet.setDefaultRowHeight((short)350);
        //合并表头表尾的单元格
        /*sheet.addMergedRegion(new CellRangeAddress(0, 2, 0, 3));
        sheet.addMergedRegion(new CellRangeAddress(3, 3, 0, 3));
        //冻结行
        workbook.getSheetAt(0).createFreezePane(0, 4);
        RegionUtil.setBorderBottom(1, new CellRangeAddress(3, 3, 0, 3), workbook.getSheetAt(0), workbook);//设置边框
*/        // 获取表头样式对象
        // 获取表体样式对象
        HSSFCellStyle style = getCommonStyle(workbook);
        
        // 定义所需列数
        int columnNum = rowsName.length;
        //创建列头
        HSSFRow rowHead = sheet.createRow(0);  
        for(int n = 0;n < columnNum;n++){
            HSSFCell cellRow = rowHead.createCell(n,HSSFCell.CELL_TYPE_STRING);//创建列头对应个数的单元格                
            cellRow.setCellValue(rowsName[n]);//设置列头单元格的值                        
            cellRow.setCellStyle(style);//设置列头单元格样式                        
        }
        
        //将查询出的数据设置到sheet对应的单元格中
        for(int i=0;i                    


                    
                

热门栏目