JAVA字符串json数组使用Formatter格式化成表格形式

需求:一个json数组,要将其解析排成表格的形式,如下:

JAVA字符串json数组使用Formatter格式化成表格形式_第1张图片

参考这个博客:https://blog.csdn.net/weixin_33778544/article/details/91427507

但这个博客没有提供名称太长时自动换行的代码,这里把所有代码贴出:

两个bean类:

/**
 * Copyright 2019 bejson.com
 */
package com.qugengting.fuck;
import java.util.List;

/**
 * Auto-generated: 2019-11-15 8:50:28
 *
 * @author bejson.com ([email protected])
 * @website http://www.bejson.com/java2pojo/
 */
public class PrintBean {

    private String orderId;
    private List bill;
    private String total;
    private String fee;
    private String payType;
    private String orderUuid;

    public String getOrderUuid() {
        return orderUuid;
    }

    public void setOrderUuid(String orderUuid) {
        this.orderUuid = orderUuid;
    }

    public String getPayType() {
        return payType;
    }

    public void setPayType(String payType) {
        this.payType = payType;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }
    public String getOrderId() {
        return orderId;
    }

    public void setBill(List bill) {
        this.bill = bill;
    }
    public List getBill() {
        return bill;
    }

    public void setTotal(String total) {
        this.total = total;
    }
    public String getTotal() {
        return total;
    }

    public void setFee(String fee) {
        this.fee = fee;
    }
    public String getFee() {
        return fee;
    }

}
/**
 * Copyright 2019 bejson.com
 */
package com.qugengting.fuck;

/**
 * Auto-generated: 2019-11-15 8:48:56
 *
 * @author bejson.com ([email protected])
 * @website http://www.bejson.com/java2pojo/
 */
public class Bill {

    private String name;
    private String price;
    private String totalPrice;
    private String count;
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }

    public void setPrice(String price) {
        this.price = price;
    }
    public String getPrice() {
        return price;
    }

    public void setTotalPrice(String totalPrice) {
        this.totalPrice = totalPrice;
    }
    public String getTotalPrice() {
        return totalPrice;
    }

    public void setCount(String count) {
        this.count = count;
    }
    public String getCount() {
        return count;
    }

}
    private String data = "{\n" +
            "    \"orderId\": \"eaedfa0001\",\n" +
            "    \"total\": \"100\",\n" +
            "    \"payType\": \"支付宝\",\n" +
            "    \"orderUuid\": \"565656786343555e\",\n" +
            "    \"bill\": [\n" +
            "        {\n" +
            "            \"name\": \"XXX即热式抗菌智能坐便器AP(限时促销)\",\n" +
            "            \"price\": \"10000\",\n" +
            "            \"totalPrice\": \"30000\",\n" +
            "            \"count\": \"3\"\n" +
            "        }\n" +
            "    ],\n" +
            "    \"fee\": \"100.00\"\n" +
            "}";

    private void print(String data) {
        PrintBean bean = new Gson().fromJson(data, PrintBean.class);
        List beans = bean.getBill();
        Bill bill = new Bill();
        bill.setName("名称");
        bill.setPrice("单价");
        bill.setCount("数量");
        bill.setTotalPrice("总价");
        beans.add(0, bill);
        Bill bill1 = new Bill();
        bill1.setName("总金额");
        bill1.setPrice("");
        bill1.setCount("");
        bill1.setTotalPrice(bean.getTotal());
        beans.add(bill1);
        Bill bill2 = new Bill();
        bill2.setName("优惠后总价");
        bill2.setCount("");
        double d1 = Double.valueOf(bean.getTotal());
        double d2 = Double.valueOf(bean.getFee());
        double d3 = d1 - d2;
        bill2.setPrice("");
        bill2.setTotalPrice(String.valueOf(d3));
        beans.add(bill2);
        printList(beans);
    }

    /**
     * 获取中文数量
     *
     * @param val
     * @return
     */
    private int getChineseNum(String val) {
        if (val == null) {
            val = "null";
        }
        String regex = "[\u4e00-\u9fa5|。|,]";
        ArrayList list = new ArrayList();
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(val);
        while (matcher.find()) {
            list.add(matcher.group());
        }
        int size = list.size();
        return size;
    }

    private int mNameWidth = 13;
    private int mNameBigWidth = 25;
    private int mPriceBigWidth = 8;
    private int mCountBigWidth = 7;
    private int mAllPriceBigWidth = 8;

    private void printList(List list) {
        StringBuilder sb = new StringBuilder();
        for (Bill bill : list) {
            //1、先处理商品名称
            int strLength = bill.getName().length();
            int index = 0;
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < strLength; i++) {
                index++;
                stringBuilder.append(bill.getName().charAt(i));

                if (index % mNameWidth == 0) {
                    stringBuilder.append("\n");
                }
            }

            //有换过行情况下
            if (stringBuilder.toString().contains("\n")) {
                String[] strings = stringBuilder.toString().split("\n");
                //最后一行处理下长度
                String lastLineStr = strings[strings.length - 1];
                int chineseNum = getChineseNum(lastLineStr);
                Formatter formatter = new Formatter();
                String val = formatter.format("%-" + (mNameBigWidth - chineseNum) + "s", lastLineStr).toString();
                //重新拼接
                for (int i = 0; i < strings.length; i++) {
                    if (i != strings.length - 1) {
                        sb.append(strings[i] + "\n");
                    } else {
                        sb.append(val);
                    }
                }
            } else {//只有一行
                int chineseNum = getChineseNum(bill.getName());
                Formatter formatter = new Formatter();
                String val = formatter.format("%-" + (mNameBigWidth - chineseNum) + "s", bill.getName()).toString();
                sb.append(val);
            }
            //2、再处理单价
            Formatter formatter = new Formatter();
            int chineseNum = getChineseNum(bill.getPrice());
            String val = formatter.format("%-" + (mPriceBigWidth - chineseNum) + "s", bill.getPrice()).toString();
            sb.append(val);
            //3、再处理数量
            Formatter formatter1 = new Formatter();
            int chineseNum1 = getChineseNum(bill.getCount());
            String val1 = formatter1.format("%-" + (mCountBigWidth - chineseNum1) + "s", bill.getCount()).toString();
            sb.append(val1);
            //4、再处理总价
            Formatter formatter2 = new Formatter();
            int chineseNum2 = getChineseNum(bill.getTotalPrice());
            String val2 = formatter2.format("%-" + (mAllPriceBigWidth - chineseNum2) + "s", bill.getTotalPrice()).toString();
            sb.append(val2);
            sb.append("\n");
        }
        System.out.println(sb.toString());
    }

    @Test
    public void test1() {
        print(data);
    }

执行test1(),即可得出以上截图效果。

 

 

你可能感兴趣的:(String及JSON操作)