这是我前台传后台的树状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;
代码如下:
主要实现逻辑 使用一段字符串接受所有的数据 然后新建一个map map键来存字段名 值存属性 当循环 发现id已经存在的时候 或者可以吧id改成你们类中的第一个字段 就反射传来的对象的所有字段,然后循环map 如果map的键== 对象的字段名 那么就给对象对应的字段名 赋值map的值
然后执行完循环反射对象的字段 并且赋值 就根据map的键不能重复特性 重新覆盖赋值
/**
* 树状List转普通List
* @param model
* @param json
* @return
* @throws ClassNotFoundException
*/
public List
2.Controller层调用
代码如下(示例):
//上面代码的类创建
TreeUtil treeUtil = new TreeUtil();
Shitongxiaoshou s = new Shitongxiaoshou();
List
然后对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;
}
这里加类型转换判断
然后 id是类的头字段 不是可能转换不正确
如果有更好的办法 希望大佬们可以告诉下我
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)