字段数组json转javabean

字段数组json转javabean
每个字段变成了单独的对象,因为要赋予其他的值,类似于

[
    {
        "prop": "zx_ylqxxkz",
        "label": "生产企业许可(备案)号",
        "show": true,
        "width": 246,
        "value": null,
        "paramType": null
    },
    {
        "prop": "ypsscyr",
        "label": "备注:",
        "show": true,
        "width": 134,
        "value": null,
        "paramType": null
    },
    {
        "prop": "ypsscyr2",
        "label": "备注:",
        "show": true,
        "width": 134,
        "value": null,
        "paramType": null
    },
   ......
]

给前台的备注,宽度值之类的,然后让后台解析成javabean,打印出来,每个字段是多少,
然后写了一个工具类,转换成javabean

package com.zp.tool;

import com.alibaba.fastjson.JSONArray;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.zp.entity.InfJcSpzlOld;
import com.zp.entity.JcDwzl;
import com.zp.entity.config.TableColumn;
//import org.apache.poi.ss.formula.functions.T;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class JSONtoJavaBean {
    public static void main(String[] args) throws ParseException, InvocationTargetException, NoSuchMethodException, IllegalAccessException, InstantiationException {
//        String string = "[{\"prop\":\"zx_ylqxxkz\",\"label\":\"生产企业许可(备案)号\",\"show\":true,\"width\":226,\"value\":null},{\"prop\":\"ypsscyr\",\"label\":\"备注:\",\"show\":true,\"width\":114,\"value\":null},{\"prop\":\"ypsscyr2\",\"label\":\"备注:\",\"show\":true,\"width\":114,\"value\":null},{\"prop\":\"zt\",\"label\":\"状态\",\"show\":true,\"width\":100,\"value\":\"N\"},{\"prop\":\"piz_no\",\"label\":\"批准文号\",\"show\":true,\"width\":128,\"value\":\"国药准字Z19991023/\"},{\"prop\":\"merge_flg\",\"label\":\"是否合并(电商使用)\",\"show\":true,\"width\":203,\"value\":\"N\"},{\"prop\":\"lius_no\",\"label\":\"流水号\",\"show\":true,\"width\":114,\"value\":20180118012917},{\"prop\":\"shangp_id\",\"label\":\"商品内码\",\"show\":true,\"width\":128,\"value\":\"103704\"},{\"prop\":\"shangp_no\",\"label\":\"商品编号\",\"show\":true,\"width\":128,\"value\":\"103704\"},{\"prop\":\"wlzx_code\",\"label\":\"物流中心编码\",\"show\":true,\"width\":156,\"value\":\"1201\"},{\"prop\":\"huoz_id\",\"label\":\"货主内码\",\"show\":true,\"width\":128,\"value\":\"1025\"},{\"prop\":\"chinese_name\",\"label\":\"中文名\",\"show\":true,\"width\":114,\"value\":\"双黄连含片\"},{\"prop\":\"zhuj_code\",\"label\":\"助记码\",\"show\":true,\"width\":114,\"value\":\"SHLHP103704\"},{\"prop\":\"yaop_guig\",\"label\":\"药品规格\",\"show\":true,\"width\":128,\"value\":\"6片*2板\"},{\"prop\":\"maker\",\"label\":\"生产厂家\",\"show\":true,\"width\":128,\"value\":\"福建太平洋制药有限公司\"},{\"prop\":\"chandi\",\"label\":\"产地\",\"show\":true,\"width\":100,\"value\":\"福建太平洋\"},{\"prop\":\"baoz_num\",\"label\":\"包装数量\",\"show\":true,\"width\":128,\"value\":320},{\"prop\":\"baoz_danw\",\"label\":\"包装单位\",\"show\":true,\"width\":128,\"value\":\"盒\"},{\"prop\":\"chaif_lid\",\"label\":\"拆分粒度\",\"show\":true,\"width\":128,\"value\":\"1\"},{\"prop\":\"kaipdw_min\",\"label\":\"最小开票单位\",\"show\":true,\"width\":156,\"value\":1},{\"prop\":\"yaop_category\",\"label\":\"药品大类\",\"show\":true,\"width\":128,\"value\":\"55\"},{\"prop\":\"cunc_condition\",\"label\":\"存储条件\",\"show\":true,\"width\":128,\"value\":\"2\"},{\"prop\":\"beactive\",\"label\":\"是否活动\",\"show\":true,\"width\":128,\"value\":\"Y\"},{\"prop\":\"zhongbz\",\"label\":\"中包装数量\",\"show\":true,\"width\":142,\"value\":1},{\"prop\":\"jixing\",\"label\":\"剂型\",\"show\":true,\"width\":100,\"value\":\"610\"},{\"prop\":\"tongy_name\",\"label\":\"通用名\",\"show\":true,\"width\":114,\"value\":\"1\"},{\"prop\":\"lot_flg\",\"label\":\"是否管理批号\",\"show\":true,\"width\":156,\"value\":\"Y\"},{\"prop\":\"zengp_flg\",\"label\":\"是否赠品\",\"show\":true,\"width\":128,\"value\":\"N\"},{\"prop\":\"jiang_flg\",\"label\":\"是否监管药品\",\"show\":true,\"width\":156,\"value\":\"N\"},{\"prop\":\"fangc_flg\",\"label\":\"是否防窜\",\"show\":true,\"width\":128,\"value\":\"N\"},{\"prop\":\"tgyp_flg\",\"label\":\"是否贵重药品\",\"show\":true,\"width\":156,\"value\":\"0\"},{\"prop\":\"jink_flg\",\"label\":\"是否进口药品\",\"show\":true,\"width\":156,\"value\":\"N\"},{\"prop\":\"yis_flg\",\"label\":\"是否易碎\",\"show\":true,\"width\":128,\"value\":\"N\"},{\"prop\":\"gzyp_flg\",\"label\":\"是否贵重药品\",\"show\":true,\"width\":156,\"value\":\"N\"},{\"prop\":\"yxp_flg\",\"label\":\"是否异形品\",\"show\":true,\"width\":142,\"value\":\"N\"},{\"prop\":\"caigou_staff\",\"label\":\"采购员\",\"show\":true,\"width\":114,\"value\":null},{\"prop\":\"ypyh_type\",\"label\":\"药品养护类型\",\"show\":true,\"width\":156,\"value\":\"1\"},{\"prop\":\"geiy_way\",\"label\":\"给药途径\",\"show\":true,\"width\":128,\"value\":null},{\"prop\":\"yaop_xingz\",\"label\":\"药品性状\",\"show\":true,\"width\":128,\"value\":null},{\"prop\":\"shiyz\",\"label\":\"适用症\",\"show\":true,\"width\":114,\"value\":null},{\"prop\":\"gengx_time\",\"label\":\"更新时间\",\"show\":true,\"width\":200,\"value\":\"2018-01-18 12:49:18\"},{\"prop\":\"yp_flg\",\"label\":\"是否药品\",\"show\":true,\"width\":128,\"value\":\"Y\"}]";
        String s="[{\"prop\":\"danw_id\",\"label\":\"单位内码\",\"show\":true,\"width\":148,\"value\":\"DWI00000001\",\"paramType\":null},{\"prop\":\"danw_no\",\"label\":\"单位编号\",\"show\":true,\"width\":148,\"value\":\"042\",\"paramType\":null},{\"prop\":\"danwsj_id\",\"label\":\"上级单位内码\",\"show\":true,\"width\":176,\"value\":\"DWI00000001\",\"paramType\":null},{\"prop\":\"yez_no\",\"label\":\"业主编号\",\"show\":true,\"width\":148,\"value\":\"007\",\"paramType\":null},{\"prop\":\"danw_name\",\"label\":\"单位名称\",\"show\":true,\"width\":148,\"value\":\"(042)天津金士力健康用品有限公司1\",\"paramType\":null},{\"prop\":\"zhuj_code\",\"label\":\"助记码\",\"show\":true,\"width\":134,\"value\":\"(042)TJJSLJKYPYXGS\",\"paramType\":null},{\"prop\":\"lot_request\",\"label\":\"批号要求\",\"show\":true,\"width\":148,\"value\":null,\"paramType\":null},{\"prop\":\"keh_category\",\"label\":\"客户分类\",\"show\":true,\"width\":148,\"value\":null,\"paramType\":null},{\"prop\":\"keh_grade\",\"label\":\"客户等级\",\"show\":true,\"width\":148,\"value\":null,\"paramType\":null},{\"prop\":\"quy_category\",\"label\":\"区域\",\"show\":true,\"width\":120,\"value\":\"0\",\"paramType\":null},{\"prop\":\"yew_staff\",\"label\":\"业务员\",\"show\":true,\"width\":134,\"value\":\"*\",\"paramType\":null},{\"prop\":\"peis_direct\",\"label\":\"配送方向\",\"show\":true,\"width\":148,\"value\":\"0000\",\"paramType\":null},{\"prop\":\"lux_no\",\"label\":\"路线编号\",\"show\":true,\"width\":148,\"value\":\"0000_01\",\"paramType\":null},{\"prop\":\"zhangc_order\",\"label\":\"装车顺序\",\"show\":true,\"width\":148,\"value\":null,\"paramType\":null},{\"prop\":\"lux_name\",\"label\":\"路线名称\",\"show\":true,\"width\":148,\"value\":null,\"paramType\":null},{\"prop\":\"lianx_staff\",\"label\":\"联系人\",\"show\":true,\"width\":134,\"value\":\"*\",\"paramType\":null},{\"prop\":\"lianxr_phone\",\"label\":\"联系人电话\",\"show\":true,\"width\":162,\"value\":\"0\",\"paramType\":null},{\"prop\":\"diz_phone\",\"label\":\"单位地址电话\",\"show\":true,\"width\":176,\"value\":\"*\",\"paramType\":null},{\"prop\":\"peiskh_flg\",\"label\":\"是否配送客户\",\"show\":true,\"width\":176,\"value\":\"Y\",\"paramType\":null},{\"prop\":\"yingyzz_flg\",\"label\":\"是否有营业执照\",\"show\":true,\"width\":190,\"value\":\"Y\",\"paramType\":null},{\"prop\":\"yingyzz_no\",\"label\":\"营业执照号\",\"show\":true,\"width\":162,\"value\":null,\"paramType\":null},{\"prop\":\"xukz_flg\",\"label\":\"是否有许可证\",\"show\":true,\"width\":176,\"value\":\"Y\",\"paramType\":null},{\"prop\":\"beactive\",\"label\":\"是否活动\",\"show\":true,\"width\":148,\"value\":\"Y\",\"paramType\":null},{\"prop\":\"peis_cd\",\"label\":\"配送编码\",\"show\":true,\"width\":148,\"value\":\"0000-0000_01-\",\"paramType\":null},{\"prop\":\"yuanckh_flg\",\"label\":\"是否远程客户\",\"show\":true,\"width\":176,\"value\":\"N\",\"paramType\":null},{\"prop\":\"dayzjbq_flg\",\"label\":\"是否打印整件标签\",\"show\":true,\"width\":204,\"value\":\"N\",\"paramType\":null},{\"prop\":\"shangc_flg\",\"label\":\"是否上传\",\"show\":true,\"width\":148,\"value\":\"N\",\"paramType\":null},{\"prop\":\"yew_category\",\"label\":\"业务类别\",\"show\":true,\"width\":148,\"value\":\"1\",\"paramType\":null},{\"prop\":\"chaxkj_flg\",\"label\":\"是否查询可见\",\"show\":true,\"width\":176,\"value\":null,\"paramType\":null},{\"prop\":\"danw_simplename\",\"label\":\"单位简称\",\"show\":true,\"width\":148,\"value\":\"*\",\"paramType\":null},{\"prop\":\"shouhuo_manager\",\"label\":\"收货负责人及联系\",\"show\":true,\"width\":204,\"value\":null,\"paramType\":null},{\"prop\":\"shouh_staff\",\"label\":\"收货员\",\"show\":true,\"width\":134,\"value\":null,\"paramType\":null},{\"prop\":\"zancqfp_type\",\"label\":\"暂存区分配类型\",\"show\":true,\"width\":190,\"value\":null,\"paramType\":null},{\"prop\":\"jituan_cd\",\"label\":\"集团编码\",\"show\":true,\"width\":148,\"value\":null,\"paramType\":null},{\"prop\":\"dengl_time\",\"label\":\"登录时间\",\"show\":true,\"width\":200,\"value\":null,\"paramType\":null},{\"prop\":\"gengx_time\",\"label\":\"更新时间\",\"show\":true,\"width\":200,\"value\":\"2014-08-26 11:01:11\",\"paramType\":null},{\"prop\":\"del_time\",\"label\":\"删除时间\",\"show\":true,\"width\":200,\"value\":null,\"paramType\":null},{\"prop\":\"yez_flg\",\"label\":\"是否业主\",\"show\":true,\"width\":148,\"value\":\"N\",\"paramType\":null},{\"prop\":\"erjfx_flg\",\"label\":\"是否二级分销客户\",\"show\":true,\"width\":204,\"value\":\"N\",\"paramType\":null},{\"prop\":\"duis_flg\",\"label\":\"赠品是否对私\",\"show\":true,\"width\":176,\"value\":\"N\",\"paramType\":null},{\"prop\":\"hed_flg\",\"label\":\"赠品是否合单\",\"show\":true,\"width\":176,\"value\":\"N\",\"paramType\":null},{\"prop\":\"jiang_flg\",\"label\":\"是否监管\",\"show\":true,\"width\":148,\"value\":\"Y\",\"paramType\":null},{\"prop\":\"zipno\",\"label\":\"邮政编码\",\"show\":true,\"width\":148,\"value\":\"0\",\"paramType\":null},{\"prop\":\"teleph\",\"label\":\"电话号码\",\"show\":true,\"width\":148,\"value\":null,\"paramType\":null},{\"prop\":\"mobile\",\"label\":\"移动电话\",\"show\":true,\"width\":148,\"value\":null,\"paramType\":null},{\"prop\":\"faxno\",\"label\":\"传真号码\",\"show\":true,\"width\":148,\"value\":null,\"paramType\":null},{\"prop\":\"province\",\"label\":\"省份\",\"show\":true,\"width\":120,\"value\":null,\"paramType\":null},{\"prop\":\"city\",\"label\":\"城市\",\"show\":true,\"width\":120,\"value\":null,\"paramType\":null},{\"prop\":\"area\",\"label\":\"地区\",\"show\":true,\"width\":120,\"value\":null,\"paramType\":null},{\"prop\":\"notes\",\"label\":\"备注\",\"show\":true,\"width\":120,\"value\":null,\"paramType\":null},{\"prop\":\"yuhai_flg\",\"label\":\"是否裕海\",\"show\":true,\"width\":148,\"value\":\"N\",\"paramType\":null},{\"prop\":\"baogd_sign\",\"label\":\"报告单标识\",\"show\":true,\"width\":162,\"value\":null,\"paramType\":null},{\"prop\":\"suos_area\",\"label\":\"所属片区\",\"show\":true,\"width\":148,\"value\":null,\"paramType\":null},{\"prop\":\"quyu_assort\",\"label\":\"区域分类\",\"show\":true,\"width\":148,\"value\":null,\"paramType\":null},{\"prop\":\"danw_identify\",\"label\":\"单位识别\",\"show\":true,\"width\":148,\"value\":null,\"paramType\":null},{\"prop\":\"pinxrq_type\",\"label\":\"内复核拼箱容器类型\",\"show\":true,\"width\":218,\"value\":\"Y\",\"paramType\":null},{\"prop\":\"yez_stocklimit\",\"label\":\"业主库存上限\",\"show\":true,\"width\":176,\"value\":0,\"paramType\":null},{\"prop\":\"dyfhdfs\",\"label\":\"打印复核单份数\",\"show\":true,\"width\":190,\"value\":1,\"paramType\":null},{\"prop\":\"zg_verify\",\"label\":\"资格审格\",\"show\":true,\"width\":148,\"value\":null,\"paramType\":null},{\"prop\":\"sf_tdyykh\",\"label\":\"是否特定医院客户\",\"show\":true,\"width\":204,\"value\":\"N\",\"paramType\":null},{\"prop\":\"dacg_suod\",\"label\":\"电子监管拦截锁定标识\",\"show\":true,\"width\":232,\"value\":\"Y\",\"paramType\":null},{\"prop\":\"yez_id\",\"label\":\"YEZ_ID\",\"show\":true,\"width\":134,\"value\":\"1610\",\"paramType\":null},{\"prop\":\"yaojno\",\"label\":\"YAOJNO\",\"show\":true,\"width\":134,\"value\":null,\"paramType\":null},{\"prop\":\"sycdw_no\",\"label\":\"上传市药采中心单位编码\",\"show\":true,\"width\":246,\"value\":null,\"paramType\":null},{\"prop\":\"carrier_name\",\"label\":\"承运商名称\",\"show\":true,\"width\":162,\"value\":null,\"paramType\":null},{\"prop\":\"carrier_id\",\"label\":\"承运商ID\",\"show\":true,\"width\":148,\"value\":null,\"paramType\":null}]";
//        InfJcSpzlOld obj=toJava(InfJcSpzlOld.class,s);
        JcDwzl obj=toJava(JcDwzl.class,s);
        System.out.println("helleJSONtoJavaBean==:"+obj);
    }
    //public static <T> T getClazz(Class<T> clazz){
    //       T t = ApplicationContextRegister.getApplicationContext().getBean(clazz);
    //     return t;
    //   };
    //单独的T 代表一个类型 而 Class<T>和Class<?>代表这个类型所对应的类
    public static <T> T toJava(Class<T> clazz, String json) throws NoSuchMethodException, ParseException, InvocationTargetException, IllegalAccessException, InstantiationException {
        String string = json;
        List<TableColumn> columnList = JSONArray.parseArray(string, TableColumn.class);
//        ObjectMapper objectMapper = new ObjectMapper();
//        for (TableColumn object : columnList) {
//            //System.out.println(object.toString());
//        }
        T trueclass= clazz.newInstance();
        //手动拼装的list《Stirng》
        List<String> setMethodList = new ArrayList<String>();
        //要赋予的值 集合 ,后面根据实际类型转
        List<String> setMethodValueList = new ArrayList<String>();
        //把数据prop和value提取出来
        for (int i = 0; i < columnList.size(); i++) {
            String prop = columnList.get(i).getProp();
            String value = columnList.get(i).getValue();
            //把set方法名先放进去
            String capitalName = StringUtils.capitalName(prop);
            String setSome = "set" + capitalName;
            setMethodList.add(setSome);
            setMethodValueList.add(value);
        }
        //获取目标类所有的方法
        Method[] methods = trueclass.getClass().getMethods();
        //过滤后的方法,只有set方法
        HashMap<String, Method> setMethodsMap = new HashMap<>();
        //只拿取set相关的 方法,
        for (Method method : methods) {
            for (String upSetMethod : setMethodList) {
                if (method.getName().equals(upSetMethod)) {
//                    System.out.println("找到对应的方法名了"+upSetMethod);
                    setMethodsMap.put(upSetMethod, method);
                }
            }
        }
        //执行set方法,给实体类赋值,
        for (int i = 0; i < setMethodList.size(); i++) {
            String pinString = setMethodList.get(i);
            Method trueMethod = setMethodsMap.get(pinString);
            if(trueMethod!=null){
                Class<?>[] getTypeParameters = trueMethod.getParameterTypes();
                //还是轮询论出来吧,
                if (getTypeParameters.length < 1) {
                    //System.out.println("此方法无参数==>" + trueMethod);
                } else {
                    //因为是实体类的set方法,参数一般只有一个,所以这里只取第一个
                    String className = getTypeParameters[0].getName();
                    //System.out.println("set方法名:" + className);
                    //先随便取一个方法,后面根据情况覆盖
                    Method centerMethod = setMethodsMap.get(pinString);
                    String value = setMethodValueList.get(i);
                    //模糊判断类型
                    if (className.contains("Long")) {
                        //如果把Person类的setName方法的访问权限设为私有 则会抛出NoSuchMethodException异常
                        centerMethod = trueclass.getClass().getMethod(setMethodList.get(i), Long.class);
                        Long longValue = Long.parseLong(value);
                        if (value != null) {
                            //如果有对应的参数,那就给赋值
                            //实现体和实际参数
                            centerMethod.invoke(trueclass, longValue);
                        }
                    } else if (className.contains("String")) {
                        centerMethod = trueclass.getClass().getMethod(setMethodList.get(i), String.class);
                        if (value != null) {
                            //如果有对应的参数,那就给赋值
                            //实现体和实际参数
                            centerMethod.invoke(trueclass, value);
                        }
                    }else if (className.contains("Integer")) {
                        centerMethod = trueclass.getClass().getMethod(setMethodList.get(i), Integer.class);
                        Integer intval = Integer.parseInt(value);
                        if (value != null) {
                            //如果有对应的参数,那就给赋值
                            //实现体和实际参数
                            centerMethod.invoke(trueclass, intval);
                        }
                    } else if (className.contains("Date")) {
                        centerMethod = trueclass.getClass().getMethod(setMethodList.get(i), Date.class);
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月份是MM
                        if(StringUtils.isNotnull(value)){
                            Date date = simpleDateFormat.parse(value);
                            Date longValue = date;
                            if (value != null) {
                                //如果有对应的参数,那就给赋值
                                //实现体和实际参数
                                centerMethod.invoke(trueclass, longValue);
                            }
                        }

                    }
                }
            }
        }

        return (T) trueclass;
    }
}

在这里插入图片描述成功转换成javabean,比较方便,如果实体类有其他不同的类型,可以自己加上,

实体类:

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import java.util.Date;

/**
 *
 * @author lsp
 *
 */
@Data
public class JcDwzl  implements java.io.Serializable  {
    @TableId(type = IdType.AUTO)
    private String danw_id; //
    private String danw_no; //
    private String danwsj_id; //
    private String yez_no; //
    private String danw_name; //
    private String zhuj_code; //
    private String lot_request; //
    private String keh_category; //
    private String keh_grade; //
    private String quy_category; //
    private String yew_staff; //
    private String peis_direct; //
    private String lux_no; //
    private String zhangc_order; //
    private String lux_name; //
    private String lianx_staff; //
    private String lianxr_phone; //
    private String diz_phone; //
    private String peiskh_flg; //
    private String yingyzz_flg; //
    private String yingyzz_no; //
    private String xukz_flg; //
    private String beactive; //
    private String peis_cd; //
    private String yuanckh_flg; //
    private String dayzjbq_flg; //
    private String shangc_flg; //
    private String yew_category; //
    private String chaxkj_flg; //
    private String danw_simplename; //
    private String shouhuo_manager; //
    private String shouh_staff; //
    private String zancqfp_type; //
    private String jituan_cd; //
    private Date dengl_time; //
    private Date gengx_time; //
    private Date del_time; //
    private String yez_flg; //
    private String erjfx_flg; //
    private String duis_flg; //
    private String hed_flg; //
    private String jiang_flg; //
    private String zipno; //
    private String teleph; //
    private String mobile; //
    private String faxno; //
    private String province; //
    private String city; //
    private String area; //
    private String notes; //
    private String yuhai_flg; //
    private String baogd_sign; //
    private String suos_area; //
    private String quyu_assort; //
    private String danw_identify; //
    private String pinxrq_type; //
    private Long yez_stocklimit; //
    private Integer dyfhdfs; //
    private String zg_verify; //
    private String sf_tdyykh; //
    private String dacg_suod; //
    private String yez_id; //
    private String yaojno; //
    private String sycdw_no; //
    private String carrier_name; //
    private String carrier_id; //
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值