栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

树状List转普通List

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

树状List转普通List

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录
  • 前言
  • 一、pandas是什么?
  • 二、使用步骤
    • 1.引入库
    • 2.读入数据
  • 总结


前言

提示:这里可以添加本文要记录的大概内容:

前台传树状List  后台将树状List转为普通List


提示:以下是本篇文章正文内容,下面案例可供参考

一、树状List数据格式

这是我前台传给我的树状list格式

[{
    "serialVersionUID": 1,
    "id": 1,
    "tid": 1,
    "cid": "A100000",
    "pid": 0,
    "vcode": null,
    "projectType": 1,
    "projectName": "视同销售(营业)收入",
    "taxAmount": 1,
    "taxAdjustmentAmount": 1,
    "childList": [{
        "serialVersionUID": 1,
        "id": 2,
        "tid": 2,
        "cid": "A100000",
        "pid": 1,
        "vcode": null,
        "projectType": 2,
        "projectName": "非货币性资产交换视同销售收入",
        "taxAmount": "222",
        "taxAdjustmentAmount": 2,
        "childList": []
    }, {
        "serialVersionUID": 1,
        "id": 3,
        "tid": 3,
        "cid": "A100000",
        "pid": 1,
        "vcode": null,
        "projectType": 3,
        "projectName": "用于市场推广或销售视同销售收入",
        "taxAmount": 3,
        "taxAdjustmentAmount": 3,
        "childList": []
    }, {
        "serialVersionUID": 1,
        "id": 4,
        "tid": 4,
        "cid": "A100000",
        "pid": 1,
        "vcode": null,
        "projectType": 4,
        "projectName": "用于交际应酬视同销售收入",
        "taxAmount": 4,
        "taxAdjustmentAmount": 4,
        "childList": []
    }, {
        "serialVersionUID": 1,
        "id": 5,
        "tid": 5,
        "cid": "A100000",
        "pid": 1,
        "vcode": null,
        "projectType": 5,
        "projectName": "用于职工奖励或福利视同销售收入",
        "taxAmount": 5,
        "taxAdjustmentAmount": 5,
        "childList": []
    }, {
        "serialVersionUID": 1,
        "id": 6,
        "tid": 6,
        "cid": "A100000",
        "pid": 1,
        "vcode": null,
        "projectType": 6,
        "projectName": "用于股息分配视同销售收入",
        "taxAmount": 6,
        "taxAdjustmentAmount": 6,
        "childList": []
    }, {
        "serialVersionUID": 1,
        "id": 7,
        "tid": 7,
        "cid": "A100000",
        "pid": 1,
        "vcode": null,
        "projectType": 7,
        "projectName": "用于对外捐赠视同销售收入",
        "taxAmount": 7,
        "taxAdjustmentAmount": 7,
        "childList": []
    }, {
        "serialVersionUID": 1,
        "id": 8,
        "tid": 9,
        "cid": "A100000",
        "pid": 1,
        "vcode": null,
        "projectType": 9,
        "projectName": "提供劳务视同销售收入",
        "taxAmount": 9,
        "taxAdjustmentAmount": 9,
        "childList": []
    }, {
        "serialVersionUID": 1,
        "id": 9,
        "tid": 10,
        "cid": "A100000",
        "pid": 1,
        "vcode": null,
        "projectType": 10,
        "projectName": "其他",
        "taxAmount": 10,
        "taxAdjustmentAmount": 10,
        "childList": []
    }]
}, {
    "serialVersionUID": 1,
    "id": 10,
    "tid": 11,
    "cid": "A100000",
    "pid": 0,
    "vcode": "",
    "projectType": 11,
    "projectName": "视同销售(营业)成本",
    "taxAmount": 11,
    "taxAdjustmentAmount": 11,
    "childList": [{
        "serialVersionUID": 1,
        "id": 11,
        "tid": 12,
        "cid": "A100000",
        "pid": 11,
        "vcode": null,
        "projectType": 12,
        "projectName": "非货币性资产交换视同销售成本",
        "taxAmount": 12,
        "taxAdjustmentAmount": 12,
        "childList": []
    }, {
        "serialVersionUID": 1,
        "id": 12,
        "tid": 13,
        "cid": "A100000",
        "pid": 11,
        "vcode": null,
        "projectType": 13,
        "projectName": "用于市场推广或销售视同销售成本",
        "taxAmount": 13,
        "taxAdjustmentAmount": 13,
        "childList": []
    }, {
        "serialVersionUID": 1,
        "id": 13,
        "tid": 14,
        "cid": "A100000",
        "pid": 11,
        "vcode": null,
        "projectType": 14,
        "projectName": "用于交际应酬视同销售成本",
        "taxAmount": 14,
        "taxAdjustmentAmount": 14,
        "childList": []
    }, {
        "serialVersionUID": 1,
        "id": 14,
        "tid": 15,
        "cid": "A100000",
        "pid": 11,
        "vcode": null,
        "projectType": 15,
        "projectName": "用于职工奖励或福利视同销售成本",
        "taxAmount": 15,
        "taxAdjustmentAmount": 15,
        "childList": []
    }, {
        "serialVersionUID": 1,
        "id": 15,
        "tid": 16,
        "cid": "A100000",
        "pid": 11,
        "vcode": null,
        "projectType": 16,
        "projectName": "用于股息分配视同销售成本",
        "taxAmount": 16,
        "taxAdjustmentAmount": 16,
        "childList": []
    }, {
        "serialVersionUID": 1,
        "id": 16,
        "tid": 17,
        "cid": "A100000",
        "pid": 11,
        "vcode": null,
        "projectType": 17,
        "projectName": "用于对外捐赠视同销售成本",
        "taxAmount": 17,
        "taxAdjustmentAmount": 17,
        "childList": []
    }, {
        "serialVersionUID": 1,
        "id": 17,
        "tid": 18,
        "cid": "A100000",
        "pid": 11,
        "vcode": null,
        "projectType": 18,
        "projectName": "用于对外投资项目视同销售成本",
        "taxAmount": 18,
        "taxAdjustmentAmount": 18,
        "childList": []
    }, {
        "serialVersionUID": 1,
        "id": 18,
        "tid": 19,
        "cid": "A100000",
        "pid": 11,
        "vcode": null,
        "projectType": 19,
        "projectName": "提供劳务视同销售成本",
        "taxAmount": 19,
        "taxAdjustmentAmount": 19,
        "childList": []
    }, {
        "serialVersionUID": 1,
        "id": 19,
        "tid": 20,
        "cid": "A100000",
        "pid": 11,
        "vcode": null,
        "projectType": 20,
        "projectName": "其他",
        "taxAmount": 20,
        "taxAdjustmentAmount": 20,
        "childList": []
    }]
}, {
    "serialVersionUID": 1,
    "id": 20,
    "tid": 21,
    "cid": "A100000",
    "pid": 0,
    "vcode": null,
    "projectType": 21,
    "projectName": "房地产开发企业特定业务计算的纳税调整额",
    "taxAmount": 21,
    "taxAdjustmentAmount": 21,
    "childList": [{
        "serialVersionUID": 1,
        "id": 21,
        "tid": 22,
        "cid": "A100000",
        "pid": 21,
        "vcode": null,
        "projectType": 22,
        "projectName": "房地产企业销售未完工开发产品特定业务计算的纳税调整额",
        "taxAmount": 22,
        "taxAdjustmentAmount": 22,
        "childList": [{
            "serialVersionUID": 1,
            "id": 22,
            "tid": 23,
            "cid": "A100000",
            "pid": 22,
            "vcode": null,
            "projectType": 23,
            "projectName": "销售未完工产品的收入",
            "taxAmount": 23,
            "taxAdjustmentAmount": 23,
            "childList": []
        }, {
            "serialVersionUID": 1,
            "id": 23,
            "tid": 24,
            "cid": "A100000",
            "pid": 22,
            "vcode": null,
            "projectType": 24,
            "projectName": "销售未完工产品预计毛利额",
            "taxAmount": 24,
            "taxAdjustmentAmount": 24,
            "childList": []
        }, {
            "serialVersionUID": 1,
            "id": 24,
            "tid": 25,
            "cid": "A100000",
            "pid": 22,
            "vcode": null,
            "projectType": 25,
            "projectName": "实际发生的税金及附加、土地增值税",
            "taxAmount": 25,
            "taxAdjustmentAmount": 25,
            "childList": []
        }]
    }, {
        "serialVersionUID": 1,
        "id": 25,
        "tid": 26,
        "cid": "A100000",
        "pid": 21,
        "vcode": null,
        "projectType": 26,
        "projectName": "房地产企业销售的未完工产品转完工产品特定业务计算的纳税调整额",
        "taxAmount": 26,
        "taxAdjustmentAmount": 26,
        "childList": [{
            "serialVersionUID": 1,
            "id": 26,
            "tid": 27,
            "cid": "A100000",
            "pid": 26,
            "vcode": null,
            "projectType": 27,
            "projectName": "销售未完工产品转完工产品确认的销售收入",
            "taxAmount": 27,
            "taxAdjustmentAmount": 27,
            "childList": []
        }, {
            "serialVersionUID": 1,
            "id": 27,
            "tid": 28,
            "cid": "A100000",
            "pid": 26,
            "vcode": null,
            "projectType": 28,
            "projectName": "转回的销售未完工产品预计毛利额",
            "taxAmount": 28,
            "taxAdjustmentAmount": 28,
            "childList": []
        }, {
            "serialVersionUID": 1,
            "id": 28,
            "tid": 29,
            "cid": "A100000",
            "pid": 26,
            "vcode": null,
            "projectType": 29,
            "projectName": "转回实际发生的税金及附加、土地增值税",
            "taxAmount": 29,
            "taxAdjustmentAmount": 29,
            "childList": []
        }]
    }]
}]

二、使用步骤 1.需要引入 
import com.alibaba.fastjson.JSON;

代码如下:

/
public List jsonToList(Object model,String json) throws ClassNotFoundException {
    int p1 = 0;
    int p2 = 0;
    int p3 = 0;
    String key = "";
    String vaule = "";
    Map map = new HashMap();
    boolean f = false;
    boolean visString = false;
    for (int i = 0;i entry : map.entrySet()){
                        if (entry.getKey().equals("serialVersionUID")){
                            continue;
                        }
                        if (field.getName().equals(entry.getKey())){
                            try {
                                if (field.getGenericType().toString().equals("class java.lang.Long")){
                                    Long lon = Long.valueOf(entry.getValue().toString());
                                    field.set(model,lon);
                                    break;
                                }
                                //Decimal类型就将int或者double转成Decimal
                                if (field.getGenericType().toString().equals("class java.math.BigDecimal")){
                                    BigDecimal bigDecimal = new BigDecimal("0.00");
                                    if (entry.getValue() instanceof String){
                                        double d = Double.parseDouble(entry.getValue().toString()) ;
                                        bigDecimal = bigDecimal.add(BigDecimal.valueOf(d));
                                    }
                                    field.set(model,bigDecimal);
                                    break;
                                }
                                field.set(model,entry.getValue());
                            } catch (IllegalAccessException e) {
                                System.out.println("转换问题");
                                e.printStackTrace();
                            }
                        }
                    }
                }
                String j = JSON.toJSONString(model);
                Object object = JSON.parseObject(j,model.getClass());
                list.add(object);
            }
            map.put(key,vaule);
        }
    }
    return list;
}
 
2.Controller层调用 

代码如下(示例):

//上面代码的类创建
TreeUtil treeUtil = new TreeUtil();
Shitongxiaoshou s = new Shitongxiaoshou();
List list = treeUtil.jsonToList(s, json);

 

然后对list  进行你所需要的业务操作

总结

这个代码需要注意的是    String类型给反射出来的字段赋值   所以会出现转换错误     类如果有特殊字段   需要在

if (field.getGenericType().toString().equals("class java.lang.Long")){
    Long lon = Long.valueOf(entry.getValue().toString());
    field.set(model,lon);
    break;
}
//Decimal类型就将int或者double转成Decimal
if (field.getGenericType().toString().equals("class java.math.BigDecimal")){
    BigDecimal bigDecimal = new BigDecimal("0.00");
    if (entry.getValue() instanceof String){
        double d = Double.parseDouble(entry.getValue().toString()) ;
        bigDecimal = bigDecimal.add(BigDecimal.valueOf(d));
    }
    field.set(model,bigDecimal);
    break;
}

这里加类型转换判断

如果有更好的办法   希望大佬们可以告诉下我

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号