转载

使用java解析json文件,并导出到excel表中

上上篇文章: 小米手机获得root权限并安装EdXposed框架(附下载链接)

上一篇文章: 使用fiddler爬取app的https包(附下载链接+图解)

上一篇文章我们已经拿到json包了,我们可能需要将它里面的一些信息整理出来,因为并不是所有的信息我们都是需要的,这里我们采用Java的环境进行对json包的解析。

  1. 准备工作

    1. 使用工具:IntelliJ IDEA
    2. 导入两个依赖:第一个是org.json包,另一个是XSSF。

      1. org.json包是一个用来beans,collections,maps,java arrays,XML和JSON互相转换的包,主要提供JSONObject和JSONArray类,是Java常用的Json解析工具。
      2. XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能。
  2. 导入json包,并且转换成项目。
  3. 获得需要的东西
  4. 导出到excel表中

1、依赖的代码

<dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20200518</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.10.1</version>
    </dependency>

2、导入的代码

public class daolu {

    public static void main(String[] args) throws IOException {


        System.out.println(readTxtFileIntoStringArrList("需要转换文件的地址"));
        List<String> a = readTxtFileIntoStringArrList("需要转换文件的地址");
        char b = '/n';
        String c = listToString3(a, b);
        //System.out.println(c);
    }

    public static List<String> readTxtFileIntoStringArrList(String filePath) {
        List<String> list = new ArrayList<String>();
        try {
            String encoding = "UTF-8";
            File file = new File(filePath);
            if (file.isFile() && file.exists()) {
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file), encoding);
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;

                while ((lineTxt = bufferedReader.readLine()) != null) {
                    list.add(lineTxt);
                }
                bufferedReader.close();
                read.close();
            } else {
                System.out.println("找不到指定的文件");
            }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }

        return list;
    }

    public static String listToString3(List list, char separator) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i));
            if (i < list.size() - 1) {
                sb.append(separator);
            }
        }
        return sb.toString();
    }



}

3、根据自己要求选择要什么数据( 可根据这个来查看json文件的结构 )

public class day13 {

    public static void main(String[] args) throws IOException {

        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("13");


        System.out.println(readTxtFileIntoStringArrList("C://Users//xft32//Desktop//13.txt"));
        List<String> a = readTxtFileIntoStringArrList("C://Users//xft32//Desktop//13.txt");
        char b = '/n';
        String c = listToString3(a, b);

        JSONObject myJSONObject;
        try {
            myJSONObject = new JSONObject(c);
            JSONObject storeInfo = myJSONObject.getJSONObject("storeInfo");
            JSONArray activities = storeInfo.getJSONArray("activities");

            for (int j = 0; j < activities.length(); j++) {
                JSONObject q = activities.getJSONObject(j);

                String description = q.getString("description");
                String icon_name = q.getString("icon_name");

                System.out.println("description=" + description);
                System.out.println("icon_name=" + icon_name);
                String name = null;
                if (q.isNull("name")) {

                } else {
                     name = q.getString("name");
                    System.out.println("name=" + name);
                }
                XSSFRow row = sheet.createRow(j);
                row.createCell(0).setCellValue("description=" + description);
                row.createCell(1).setCellValue("icon_name=" + icon_name);
                row.createCell(2).setCellValue("name=" + name);

            }

        } catch (JSONException e) {
            e.printStackTrace();
            System.out.println("异常");
        }

        FileOutputStream out = new FileOutputStream("C://Users//xft32//Desktop//13.xlsx");
        workbook.write(out);
        out.flush();
        out.close();
        System.out.println("写入成功");
    }

4、导出到excel表

public class day14 {
    public static void main(String[] args) throws IOException {

        XSSFWorkbook workbook =new XSSFWorkbook();//创建新的excel表
        XSSFSheet sheet =workbook.createSheet("13");//工作表标签命名
        XSSFRow row=sheet.createRow(0);//第几行
        row.createCell(0).setCellValue("");//第几个格子,内容是什么


        FileOutputStream out=new FileOutputStream("C://Users//xft32//Desktop//13.xlsx");/输出位置和名字命名
        workbook.write(out);
        out.flush();
        out.close();
        System.out.println("写入成功");
    }
}

利用这个方法我们可以更有效的完成数据的收集。

原文  https://segmentfault.com/a/1190000023300992
正文到此结束
Loading...