zoukankan      html  css  js  c++  java
  • 『类型转换』Object转Map、Map转Object

    fastjson

    User user = new User();
    user.setName("Java");
    user.setAge(26);

    //Object转Map Map map = JSONObject.parseObject(JSONObject.toJSONString(user), Map.class);
    Map<String,Object> map = JSONObject.parseObject(JSON.toJSONString(user));
    //Map转Object User user = JSON.parseObject(JSON.toJSONString(map), User.class); User user = JSONObject.toJavaObject(JSON.toJSONString(map), User.class);

    jackson

    String转Map<String,Map<String,Double>>
    String test;
    Map<String,Map<String,Double>> map = new HashMap<>();
    ObjectMapper mapper = new ObjectMapper();
    try{
      map = mapper.readValue(tset,new TypeReference<Map<String,Map<String,Double>>>(){});
    }catch(Exception e){
        e.printStackTrace();
    }


    //对象转map
    Map m = mapper.readValue(mapper.writeValueAsString(user), Map.class);

    //map转对象
    User user = mapper.readValue(mapper.writeValueAsString(m), User.class);

    还可以用org.apache.commons.beanutils.BeanMap进行转换

    Map<String, Object> map = new org.apache.commons.beanutils.BeanMap(user);

    还可以用org.apache.commons.beanutils.BeanUtils将map转为对象

    BeanUtils.populate(user, map);
  • 相关阅读:
    4500 小Q系列故事——屌丝的逆袭
    HDU 1171 Big Event in HDU
    linux库文件 /usr/lib
    HDU 1860 统计字符
    编程之美~~传话游戏
    HDU 1087 Super Jumping! Jumping! Jumping!
    HDU 1203 I NEED A OFFER!
    各种树
    HDU 3127 WHUgirls
    01背包 完全背包 多重背包 二维费用背包
  • 原文地址:https://www.cnblogs.com/yan-sh/p/13718671.html
Copyright © 2011-2022 走看看