• 请不要在回答技术问题时复制粘贴 AI 生成的内容
rizon
V2EX  ›  程序员

Java 对象转 map 有什么好用的工具吗?

  •  
  •   rizon ·
    othorizon · Jul 23, 2019 · 14877 views
    This topic created in 2671 days ago, the information mentioned may be changed or developed.

    使用各种 json 工具先转字符串 json 再转 map 也可以,但是还得处理日期啊什么的这些特殊字符,而且这种方式也很难受啊。

    有什么好用的工具吗?

    31 replies  •  2019-07-24 22:41:00 +08:00
    jamesliu96
    jamesliu96
    Jul 23, 2019 via Android
    1
    我以前是自己用反射写,也是苦于找不到能完成需求的工具
    chendy
    chendy
    Jul 23, 2019
    2
    好奇什么场景…会有这种需求
    BBCCBB
    BBCCBB
    Jul 23, 2019
    3
    Jackson 就可以
    BBCCBB
    BBCCBB
    Jul 23, 2019
    4
    有 convert 的 api.
    BBCCBB
    BBCCBB
    Jul 23, 2019
    5
    或者 dozer 也可以,不过 jackson 就可以满足需求,没必要引入 dozer
    qwerthhusn
    qwerthhusn
    Jul 23, 2019❤️1
    6
    public static Map<String, Object> main(Object args) {
    return Arrays.stream(BeanUtils.getPropertyDescriptors(args.getClass()))
    .filter(pd -> !"class".equals(pd.getName()))
    .collect(HashMap::new,
    (map, pd) -> map.put(pd.getName(), ReflectionUtils.invokeMethod(pd.getReadMethod(), args)),
    HashMap::putAll);
    }

    写的,很简单,前提是用了 JDK8 或者以上,用了 Spring
    cubecube
    cubecube
    Jul 23, 2019 via Android
    7
    没有太好的,都有很大的性能损耗。不在乎的话,json 就行。或者 beanutils
    在乎的话,mapstruct
    passerbytiny
    passerbytiny
    Jul 23, 2019
    8
    需求场景几乎不存在,自己用反射写吧。
    qwerthhusn
    qwerthhusn
    Jul 23, 2019
    9
    @jamesliu96 6 楼满足你的需求
    wysnylc
    wysnylc
    Jul 23, 2019
    10
    最简单的方式,转成 json 字符串再反序列化
    springmarker
    springmarker
    Jul 23, 2019
    11
    自己写反射咯,当然还得考虑父类的字段
    leafShimple
    leafShimple
    Jul 23, 2019
    12
    fastjson 直接用
    letitbesqzr
    letitbesqzr
    Jul 23, 2019
    13
    hutool 工具类中 BeanUtil.beanToMap

    也是通过反射实现
    cheng6563
    cheng6563
    Jul 23, 2019 via iPhone
    14
    fastjson 直接转成 JSONObject,就是一个 map
    cheng6563
    cheng6563
    Jul 23, 2019 via iPhone
    15
    fastjson 转了之后还有一堆入 getIntValue 之类的实用方法,真心觉得 fastjson 的 API 是最好用的了
    CoderGeek
    CoderGeek
    Jul 23, 2019
    16
    CGLIB BeanMap
    cassyfar
    cassyfar
    Jul 24, 2019
    17
    Jackson
    Variazioni
    Variazioni
    Jul 24, 2019
    18
    fastjson 来回倒腾转一下就行。。。挺好用的。。
    fff333
    fff333
    Jul 24, 2019
    19
    推荐 gson,fast 经常爆漏洞
    HolmLoh
    HolmLoh
    Jul 24, 2019 via Android
    20
    反射吧,我都是写好抽象类,需要转 map 的话直接继承的
    niaobulashi
    niaobulashi
    Jul 24, 2019
    21
    ```
    /**
    * java bean 转 Map
    * @param bean
    * @param propNames 需要放到 map 中的属性名称
    * @return
    */
    public static Map<String,Object> beanToMap(Object bean, String...propNames) {
    Map<String,Object> rtn = new HashMap<String,Object>();
    if(ArrayUtils.isEmpty(propNames)){
    List<String> fieldNames = getFieldNames(bean.getClass());
    for (String fieldName: fieldNames) {
    Object value = getProperty(bean, fieldName);
    rtn.put(fieldName, value);
    }
    }else{
    for(String propName: propNames){
    Object value = getProperty(bean, propName);
    rtn.put(propName, value);
    }
    }
    return rtn;
    }
    ```
    niaobulashi
    niaobulashi
    Jul 24, 2019
    22
    emmmm,评论不能用 md 格式了🐎
    brust
    brust
    Jul 24, 2019
    23
    Gson,FastJSON
    zhybb2010
    zhybb2010
    Jul 24, 2019
    24
    不要管性能,速度,拿起 fastjson 就是一套 comb。
    wo0uewjuoi
    wo0uewjuoi
    Jul 24, 2019
    25
    @fff333 前几个礼拜刚刚按要求升级 fast 版本防漏洞。。。
    netsail
    netsail
    Jul 24, 2019
    26
    @rizon 可以用 6 楼的方法
    quadrapop
    quadrapop
    Jul 24, 2019
    27
    6 楼写法挺大佬的
    xuanbg
    xuanbg
    Jul 24, 2019
    28
    用 json 序列化对象再反序列化成 Map 呀,还要什么自行车。。。话说我们对象复制、对象转换也是这样搞,爽歪歪
    twoyuan
    twoyuan
    Jul 24, 2019
    29
    Kotlin 的话就定义个扩展函数的事:

    fun Any.objToMap() = this::class.declaredMemberProperties.map { it.name to it.call(this) }.toMap()
    LeeSeoung
    LeeSeoung
    Jul 24, 2019
    30
    使用 beanutils.describe
    abai
    abai
    Jul 24, 2019
    31
    BeanMap
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1923 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 90ms · UTC 02:31 · PVG 10:31 · LAX 18:31 · JFK 21:31
    ♥ Do have faith in what you're doing.