ReadLocalJsonFileDemo.java 6.28 KB
package com.glsk.compute.brand;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import lombok.Data;

import java.io.*;
import java.util.ArrayList;
import java.util.List;


/**
 * @author fangfang
 * @create 2023-07-28 14:09
 * 获取门店 区域(省份)  品牌得映射关系
 */
public class ReadLocalJsonFileDemo {
    public static void main(String[] args) throws IOException {
//        File file = new File("D:\\test.json");
        File file = new File("src/main/resources/config/品牌.json");
        JSONObject jsonObject = readerMethod2(file);



         // 读取json文件的data字段,并转换为json数组
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println("======================");

        JSONArray array = jsonObject.getJSONArray("data");
        List<Brand> brandList=new ArrayList<>();
        for (int i = 0; i < array.size(); i++) {
            JSONObject json = array.getJSONObject(i);
            System.out.println("id="+json.getString("id"));


            JSONArray children = JSON.parseArray(json.getString("children"));
            if(JSON.parseArray(json.getString("children"))!=null){
                 for (int j = 0; j < children.size(); j++) {
                     Brand brand =new Brand();
                     brand.setId( json.getString("id"));
                     brand.setLabel(json.getString("label"));
                     brand.setLogoUrl(json.getString("logoUrl"));

                     JSONObject childrenjson = children.getJSONObject(j);
                     brand.setChildrenid(childrenjson.getString("id"));
                     brand.setChildrenlabel(childrenjson.getString("label"));
                     brand.setChildrenlogoUrl(childrenjson.getString("logoUrl"));
                     brand.setChildrenchildren(childrenjson.getString("children"));
                     brandList.add(brand);
                 }
             }
        }

//        System.out.println(brandList.toString());

        String fileName="src/main/resources/resoult/brandList.xlsx";
        EasyExcel.write(fileName,Brand.class).sheet("Sheet1").doWrite(brandList);


    }


  public static JSONObject readerMethod(File file) throws IOException {
        FileReader fileReader = new FileReader(file);
        Reader reader = new InputStreamReader(new FileInputStream(file), "Utf-8");
        int ch= 0;
        StringBuffer sb = new StringBuffer();
        while((ch = reader.read()) != -1) {
            sb.append((char) ch);
        }
        fileReader.close();
        reader.close();
        String jsonStr = sb.toString();
        return JSONObject.parseObject(jsonStr);
    }





  public static JSONObject readerMethod2(File file) throws IOException {
      String filePath = "path/to/your/file.json";

      StringBuilder contentBuilder = new StringBuilder();
      try (BufferedReader br = new BufferedReader(new FileReader(file))) {
          String line;
          while ((line = br.readLine()) != null) {
              contentBuilder.append(line).append("\n");// 获取StringBuilder对象的字符串表示形式
          }
      } catch (IOException e) {
          e.printStackTrace();
      }

      JSONObject jsonObj = JSONObject.parseObject(contentBuilder.toString())  ; // 将字符串解析为JSONObject对象

      // 打印结果
      System.out.println(jsonObj);

        return jsonObj;
    }


}

@Data
class Brand{
    @ColumnWidth(10)
    @ExcelProperty({"品牌id"})
    private  String id ;
    @ColumnWidth(10)
    @ExcelProperty({"品牌名"})
    private  String  label ;
    @ColumnWidth(10)
    @ExcelProperty({"logo地址"})
    private  String logoUrl;
    @ColumnWidth(10)
    @ExcelProperty({"子品牌id"})
    private  String childrenid ;
    @ColumnWidth(10)
    @ExcelProperty({"子品牌名称"})
    private  String  childrenlabel ;
    @ColumnWidth(10)
    @ExcelProperty({"子品牌logo"})
    private  String childrenlogoUrl;
    @ColumnWidth(10)
    @ExcelProperty({"子品牌"})
    private  String childrenchildren;

    public String getChildrenid() {
        return childrenid;
    }

    public void setChildrenid(String childrenid) {
        this.childrenid = childrenid;
    }

    public String getChildrenlabel() {
        return childrenlabel;
    }

    public void setChildrenlabel(String childrenlabel) {
        this.childrenlabel = childrenlabel;
    }

    public String getChildrenlogoUrl() {
        return childrenlogoUrl;
    }

    public void setChildrenlogoUrl(String childrenlogoUrl) {
        this.childrenlogoUrl = childrenlogoUrl;
    }

    public String getChildrenchildren() {
        return childrenchildren;
    }

    public void setChildrenchildren(String childrenchildren) {
        this.childrenchildren = childrenchildren;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getLogoUrl() {
        return logoUrl;
    }

    public void setLogoUrl(String logoUrl) {
        this.logoUrl = logoUrl;
    }


}

class ChildrenBrand {
    @ColumnWidth(10)
    @ExcelProperty({"子品牌id"})
    private  Integer id ;
    @ColumnWidth(10)
    @ExcelProperty({"子品牌名称"})
    private  String  label ;
    @ColumnWidth(10)
    @ExcelProperty({"子品牌logo"})
    private  String logoUrl;
    @ColumnWidth(10)
    @ExcelProperty({"子品牌"})
    private  String children;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getLogoUrl() {
        return logoUrl;
    }

    public void setLogoUrl(String logoUrl) {
        this.logoUrl = logoUrl;
    }

    public String getChildren() {
        return children;
    }

    public void setChildren(String children) {
        this.children = children;
    }
}