fastjson解析json文件并导出excel表格

一、工程建立及jar导入
fastjson解析json文件并导出excel表格_第1张图片
导入jar包.png

二、直接贴简单的学习代码

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class ReadFile {
     public void reader(String filePath) {
            try {
                File file = new File(filePath);
                if (file.isFile() && file.exists()) {
                    InputStreamReader read = new InputStreamReader(new FileInputStream(file), "UTF-8");
                    BufferedReader br = new BufferedReader(read);
                    String s = "";
                    String tmpstr = "";
                    while ((s = br.readLine()) != null) {
                        tmpstr += s;
                    }
                    if (tmpstr != null) {
                        process(tmpstr);
                    } 
                    System.out.println("--- end ---");
                }
            }catch (IOException e) {
                System.out.println("Error reading file content!");
                e.printStackTrace();
            }
            
        }
     
     /**
      * {
            success: true,
            root: [{
                type: 'long',
                propName: 'xmpp.nat.serverport',
                location: '0',
                propValue: '5228',
                desc: '打洞服务器端口 5228(缺省)'
            }, {
                type: 'char',
                propName: 'xmpp.security.default.pi.level',
                location: '0',
                propValue: '-1',
                desc: '缺省主控方继承级别 -1表示继承所有顶级部门权限(缺省) 0只是继承一级部门'
            }]
        }
      * @param txtStr
      */
     
     private static void process(String txtStr) {
            JSONObject json = JSONObject.parseObject(txtStr);
            Object datas = json.get("root");
            List root_list = JSON.parseArray(datas+"", Property.class);
            //创建工作簿
            HSSFWorkbook workBook = new HSSFWorkbook();
            //创建工作表  工作表的名字叫helloWorld
            HSSFSheet sheet = workBook.createSheet("系统属性");
            for(int i=0; i

你可能感兴趣的:(fastjson解析json文件并导出excel表格)