精华内容
下载资源
问答
  • ObjectMapper 对象,字符串,Map 之间的相互转换 相关jar com.fasterxml.jackson.core jackson-databind 2.9.9.3 package com.kyexpress.json.json; import java.text.SimpleDateFormat; import java.util....

    ObjectMapper 对象,字符串,Map 之间的相互转换
    一、objectMapper

    https://blog.csdn.net/han1196639488/article/details/79358743

    二、JsonNode、JsonObject常用方法

    https://blog.csdn.net/mst1010/article/details/78589059

    三、Json 和 JsonNode 区别 和 转换

    https://blog.csdn.net/luhanqiang/article/details/65937951

    四、JsonObject 和 ObjectMapper

    https://blog.csdn.net/u011983111/article/details/81389617

    五、最多用的

    https://blog.csdn.net/qq_39938758/article/details/79296461
    相关jar

    com.fasterxml.jackson.core
    jackson-databind
    2.9.9.3

    package com.kyexpress.json.json;

    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;

    import com.fasterxml.jackson.databind.DeserializationFeature;
    import com.fasterxml.jackson.databind.JsonNode;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.SerializationFeature;
    import com.kyexpress.json.model.User;

    public class ObjectMapperUtil {
    private static ObjectMapper objectMapper;

    static {
    	objectMapper = new ObjectMapper();
    	// 转换为格式化的json
    	objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
    	// 如果json中有新增的字段并且是实体类类中不存在的,不报错
    	objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }
    
    // josn 2 object
    
    public static void object2Json() throws Exception {
    	User user = new User();
    	user.setAddress("深圳");
    	user.setDataTime("2019-05-08 16:44:33");
    	user.setId("1");
    	user.setPasswd("admin123");
    	user.setUserName("admin");
    	System.out.println("object2Json:" + objectMapper.writeValueAsString(user));
    }
    
    // list 2 json
    public static void list2Json() throws Exception {
    	List<User> users = new ArrayList<User>();
    	User user = null;
    	for (int i = 1; i < 5; i++) {
    		user = new User();
    		user.setAddress("深圳-" + i);
    		user.setDataTime("2019-05-08 16:44:33");
    		user.setId("1-" + i);
    		user.setPasswd("admin123-" + i);
    		user.setUserName("admin-" + i);
    		users.add(user);
    	}
    
    	System.out.println("list2Json:" + objectMapper.writeValueAsString(users));
    
    	List<User> users2 = objectMapper.readValue(objectMapper.writeValueAsString(users), List.class);
    	System.out.println("json2list:" + users2.get(0));
    	String u = objectMapper.writeValueAsString(users2.get(0));
    	System.out.println("data:" + u);
    	User use = objectMapper.readValue(u, User.class);
    	System.out.println(use.getAddress());
    }
    
    // string 2 object
    public static void string2Object() throws Exception {
    	String data = "{\"id\" : \"1\", \"userName\" : \"admin\",\"passwd\" : \"admin123\", \"address\" : \"深圳\", \"dataTime\" : \"2019-05-08 16:44:33\"}";
    	User use = objectMapper.readValue(data, User.class);
    	System.out.println(use.getDataTime());
    }
    
    // map to json
    public static void map2Json() throws Exception {
    	Map<String, Object> testMap = new HashMap<String, Object>();
    	testMap.put("name", "merry");
    	testMap.put("age", 30);
    	testMap.put("date", new Date());
    	System.out.println("map2Json:" + objectMapper.writeValueAsString(testMap));
    	Map<String, Object> testMapDes = objectMapper.readValue(objectMapper.writeValueAsString(testMap), Map.class);
    	System.out.println(testMapDes.get("age"));
    }
    
    // 日期字段转换
    public static void dataFormat() throws Exception {
    	objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM"));
    	User user = new User("1", "admin", new Date());
    	String jsonStr = objectMapper.writeValueAsString(user);
    	System.out.println("对象转为字符串:" + jsonStr);
    }
    
    // json字符串转换为jsonNode
    public static void json2JsonNode() throws Exception {
    	String data = "{\"id\" : \"1\", \"userName\" : \"admin\",\"passwd\" : \"admin123\", \"address\" : \"深圳\", \"dataTime\" : \"2019-05-08 16:44:33\"}";
    	JsonNode rootNode = objectMapper.readTree(data);
    	System.out.println(rootNode.get("userName").asText());
    }
    
    public static void main(String[] args) throws Exception {
    	json2JsonNode();
    }
    

    }

    展开全文
  • ObjectMapper将复杂Map转换为实体类

    千次阅读 2020-09-28 10:25:32
    背景:用fastjson转换复杂Map是浅层的可以转 深层的转换为null.,这时候可以用jackson来转。 1.使用ObjectMapper要添加的依赖是 <!--添加jackson包--> <dependency> <groupId>...

    背景:用fastjson转换复杂Map是浅层的可以转 深层的转换为null.,这时候可以用jackson来转。
    1.使用ObjectMapper要添加的依赖是

           <!--添加jackson包-->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.8.3</version>
            </dependency>
    

    2.将复杂Map开始转换

    ObjectMapper mapper = new ObjectMapper();
    Resource resource = mapper.convertValue(Map, Resource.class);
    

    注意的点:实体类中的字段是驼峰的 首字母不能大写。

    展开全文
  • 使用jackson工具类objectMapper对象实现 json字符串与对象bean、map、list互相转换 工具类: // spring自带的jackson工具类,可以使用它进行序列化实现格式转换 private static final ObjectMapper MAPPER = new ...

    使用jackson工具类objectMapper对象实现 json字符串与对象bean、map、list互相转换

    工具类:

      // spring自带的jackson工具类,可以使用它进行序列化实现格式转换
      private static final ObjectMapper MAPPER = new ObjectMapper();
    

    1.对象转json字符串

    User user = new User();
    String userJson = MAPPER.writeValueAsString(user);
    

    2.Map转json字符串

    Map map = new HashMap();  
    String json = MAPPER.writeValueAsString(map);
    

    3.数组list转json字符串

    Student[] stuArr = {student1, student3};  
    String jsonfromArr =  mapper.writeValueAsString(stuArr);
    

    4.json字符串转对象

    String expected = "{\"name\":\"zhangsan\"}";
    User user = mapper.readValue(expected, User.class);
    

    5.json字符串转Map

    String expected = "{\"name\":\"zhangsan\"}";
    Map userMap = mapper.readValue(expected, Map.class);
    

    6.json字符串转对象数组List

    String expected="[{\"a\":12},{\"b\":23},{\"name\":\"Ryan\"}]";
    CollectionType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, User.class);
    List<User> userList = mapper.readValue(expected, listType);
    

    7.json字符串转Map数组List<Map<String,Object>>

    String expected="[{\"a\":12},{\"b\":23},{\"name\":\"Ryan\"}]";
    CollectionType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, Map.class);
    List<Map<String,Object>> userMapList = mapper.readValue(expected, listType);
    

    8.jackson默认将对象转换为LinkedHashMap:

    String expected = "[{\"name\":\"Ryan\"},{\"name\":\"Test\"},{\"name\":\"Leslie\"}]";
    ArrayList arrayList = mapper.readValue(expected, ArrayList.class);
    

    9.json字符串与list或map互转的方法

    ObjectMapper objectMapper = new ObjectMapper();
     //遇到date按照这种格式转换
     SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     objectMapper.setDateFormat(fmt);
     
      String preference = "{name:'zhangsan'}";
            //json字符串转map
      Map<String, String> preferenceMap = new HashMap<String, String>();
      preferenceMap = objectMapper.readValue(preference, preferenceMap.getClass());
      
      //map转json字符串
      String result=objectMapper.writeValueAsString(preferenceMap);
    

    10.bean转换为map

    List<Map<String,String>> returnList=new ArrayList<Map<String,String>>();
    List<Menu> menuList=menuDAOImpl.findByParentId(parentId);
    ObjectMapper mapper = new ObjectMapper();
    //用jackson将bean转换为map
    returnList=mapper.convertValue(menuList,new TypeReference<List<Map<String, String>>>(){});
    
    展开全文
  • 依赖包 1.jackson-core-asl-1.9.13.jar 2.jackson-mapper-asl-1.9.13.jar public class Test { public static void main(String[] args) throws Exception ... ObjectMapper o = new ObjectMapper(); Student s...

    依赖包
    1.jackson-core-asl-1.9.13.jar
    2.jackson-mapper-asl-1.9.13.jar

    public class Test {
    
    	public static void main(String[] args) throws Exception
    	{
    		ObjectMapper o = new ObjectMapper();
        	Student std = new Student("张三",20);
    		String out1 = o.writeValueAsString(std);
    		System.out.println(out1);
    		Map<String,Integer> map = new HashMap<String,Integer>();
    		map.put("李四", 30);
    		map.put("王五", 40);
    		String out2 = o.writeValueAsString(map);
    		System.out.println(out2);
    	}
    }
    
    {"name":"张三","age":20}
    {"李四":30,"王五":40}
    
    展开全文
  • Jackson之ObjectMapper对象的使用

    千次阅读 多人点赞 2019-08-21 14:42:07
    文章目录1 简介2 Jackson的数据绑定3 Jackson ObjectMapper对象示例4 反序列化ObjectMapper从JSON属性匹配到Java属性的过程从JSON字符串读取Java对象从JSON Reader对象读取Java对象从JSON文件读取Java对象从URL获取...
  • timestamp类型字段,有的是差8个时区,有的居然差9个时区,代码截图如下,求大神解答一下 ![![图片说明](https://img-ask.csdn.net/upload/201810/16/1539667381_480811.png)![!图片说明]...
  • 自己做了一java对象转换为json对象的小示例:JacksonTest.javaimport java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;import org.codehaus.jackson.map.ObjectMapper...
  • 一、创建对象并初始化 public class JsonTest { private static ObjectMapper objectMapper = new ObjectMapper(); static { //config objectMapper.enable(SerializationFeature.INDENT_OUTPUT); // 如果...
  • Jackson中的ObjectMapper对象的使用详解

    千次阅读 2019-01-07 11:42:09
    写在前面:开发中经常用到json和对象的相互转换,下面将列出FastJson中ObjectMapper对象的API的使用 一、maven工程中pom导入 &lt;dependency&gt; &lt;groupId&gt;...jackson-databin...
  • 写在前面:开发中经常用到json和对象的相互转换,下面将列出FastJson中ObjectMapper对象的API的使用 一、maven工程中pom导入 <dependency> <groupId>com.fasterxml.jackson.core</groupId> ...
  • 大家好,我是烤鸭: 采坑实录,想把json数据直接转成对象,其中有个属性是list<T>: 异常 1 ...
  • 1、要想将Java对象转化成JSON字符...import org.codehaus.jackson.map.ObjectMapper; public static void registerUser(User user) { try { ObjectMapper mapper = new ObjectMapper(); String json = mapper.wri
  • json1串: {"orderCount":0,"signType":"SHA256WithRSA","sign":"85022E420F6F0429A2A1F...调用者获取json2串使用ObjectMapper转换Map便会报上述错误。 解决方法:  直接调用sendText方法发出响应。
  • 使用ObjectMapper转换JSON

    2019-11-21 22:40:37
    文章目录maven 依赖使用场景测试model类使用场景实例1、Java对象转换为JSON对象2、JSON对象转换为Java对象3、Java数组对象和JSON数组对象转换4、抽取json中的部分数据进行转换 ObjectMapper 是一个使用 Swift 编写的...
  • 若后台对象属性名首字母为大写,前端的对象的属性名首字母必须为小写。不然会报: 而对象转json字符串时,对象中的NodeName首字母是大写,转出来是小写,在前端获取的就是nodeName 前端对象/对象数组转json字符串...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 8,132
精华内容 3,252
关键字:

objectmapper对象转换map