精华内容
下载资源
问答
  • ObjectMapper 主要用作java对象的json化和返json化 看示例: public class ObjectMapperDemo { public ObjectMapper initObject(){ ObjectMapper objectMapper = new ObjectMapper(); // 转换为格式化的json ...

    ObjectMapper 主要用作java对象的json化和返json化

    看示例:

    public class ObjectMapperDemo {
    
        public ObjectMapper initObject(){
            ObjectMapper objectMapper = new ObjectMapper();
            // 转换为格式化的json
            objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
            //新增属性不报错
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            return objectMapper;
        }
    
        public static void main(String [] args) throws IOException {
            Person person = new Person(30, "qobin");
            ObjectMapper objectMapper = new ObjectMapperDemo().initObject();
            System.out.println(objectMapper.writeValueAsString(person));
            objectMapper.writeValue(new FileWriter(new File("E:\\demo.txt"), true), person);
        }
    }
    
    展开全文
  • 摘抄的ObjectMapper用法

    千次阅读 2016-05-18 15:41:27
    ObjectMapper使用小例子: 首先有个实体对象: public class User implements Serializable { /** * */ private static final long serialVersionUID = 1L; private String username; private int age; ...

    ObjectMapper使用小例子:

    首先有个实体对象:

    public class User implements Serializable {
    /**
    *
    */
    private static final long serialVersionUID = 1L;
    private String username;
    private int age;
    private String[] list;
    private Date datetime;
    <.....省略set get.....>
    }

    先创建个对象

          static ObjectMapper omapper = new ObjectMapper();

    一、对象转换为json数据

        1.  User u = new User();
         u.setAge(24);
         u.setUsername("dobble");
         omapper.writeValue(new File("D:/aa.txt"), u);//写到文件中
         omapper.writeValue(System.out, u);//输出到控制台
            
            输出:{"username":"dobble","age":24,"list":null,"datetime":null}
    
        2.  List<Integer> list=new ArrayList<Integer>();
         list.add(1);
         list.add(2);
         list.add(3);
         String result = omapper.writeValueAsString(list);//序列化
         System.out.println(result);
    
            输出:[1,2,3]
    
        3.  omapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);<1>
    
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");<2>
            omapper.setDateFormat(df);
    
            User uu = new User();
         uu.setAge(24);
         uu.setDatetime(new Date());
         String[] sts = {"a1","a2","a3"};
         uu.setList(sts);
         uu.setUsername("json");
            String r1 = omapper.writeValueAsString(uu);
         System.out.println(r1);
            
            输出1:不加时间格式为-------{"username":"json","age":24,"list":["a1","a2","a3"],"datetime":1463552017876}
                   加了设置<1>为--------{"username":"json","age":24,"list":["a1","a2","a3"],"datetime":"2016-05-18T06:26:13.414+0000"}
                   加了自定义<<2>格式为-{"username":"json","age":24,"list":["a1","a2","a3"],"datetime":"2016-05-18 02:30:51"}
    
        4.  JsonGenerator jg = omapper.getFactory().createGenerator(System.out, JsonEncoding.UTF8);
         jg.writeObject(uu); //用的是例3的uu对象
         System.out.println();
    
            输出:加了时间格式---{"username":"json","age":24,"list":["a1","a2","a3"],"datetime":"2016-05-18 02:36:46"}
    
     

    2.json数据转换为对象

            File ff = new File("D:/aa.txt");
            String str = FileUtils.readFileToString(ff);//可以直接读文件 文件中为json数据
    //        String jsonStr = "{\"username\":\"misty\",\"age\":\"24\"}";//也可以自己写json数据
            User user = omapper.readValue(str, User.class);
            System.out.println(user.getAge()+"--"+user.getUsername());   
            
            输出为:24--dobble
    
    获取复杂json数据的特定数据:
            String test="{"results":[
                         {"objectID":357,"geoPoints":[{"x":504604.59802246094,"y":305569.9150390625}]},
                         {"objectID":358,"geoPoints":[{"x":504602.2680053711,"y":305554.43603515625}]}
                         ]}";//双引号要加转义字符,为了方便可以放到文件中读取
    
            String test = FileUtils.readFileToString(new File("D:/aa.txt")); //将上面字符串放到aa.txt中                                  
            JsonNode jnode = omapper.readTree(test);
         JsonNode node = jnode.get("results");
         for(int i=0;i<node.size();i++){
           System.out.println(node.get(i).get("objectID"));
                JsonNode no= node.get(i).get("geoPoints");
           for(int j=0;j<no.size();j++){
             double n1 = no.get(j).get("x").doubleValue();
             double n2 = no.get(j).get("y").doubleValue();
             System.out.println(n1 + "--" + n2);
           }
         } 
            输出为:357
                    504604.59802246094--305569.9150390625
                    358
                    504602.2680053711--305554.43603515625
    


    展开全文
  • ObjectMapper常见用法

    2020-08-15 00:29:23
    从字符串中读取JSONObjectMapper objectMapper = new ObjectMapper(); String carJson = “{ “brand” : “Mercedes”, “doors” : 5 }”; Car car = objectMapper.readValue(carJson, Car.class);从Reader中读取...

    从字符串中读取JSON

    ObjectMapper objectMapper = new ObjectMapper();
    

    String carJson =

    “{ “brand” : “Mercedes”, “doors” : 5 }”;

    Car car = objectMapper.readValue(carJson, Car.class);

    从Reader中读取JSON

    ObjectMapper objectMapper = new ObjectMapper();

    String carJson =

    “{ “brand” : “Mercedes”, “doors” : 4 }”;

    Reader reader = new StringReader(carJson);

    Car car = objectMapper.readValue(reader, Car.class);

    从File中读取JSON

    ObjectMapper objectMapper = new ObjectMapper();

    File file = new File(“data/car.json”);

    Car car = objectMapper.readValue(file, Car.class);

    从URL中读取JSON

    ObjectMapper objectMapper = new ObjectMapper();

    URL url = new URL(“file:data/car.json”);

    Car car = objectMapper.readValue(url, Car.class);

    InputStream中读取JSON

    ObjectMapper objectMapper = new ObjectMapper();

    InputStream input = new FileInputStream(“data/car.json”);

    Car car = objectMapper.readValue(input, Car.class);


    objectMapper.readValue(request.getInputStream(), Car.class);

    Byte数组中读取JSON

    ObjectMapper objectMapper = new ObjectMapper();

    String carJson =

    “{ “brand” : “Mercedes”, “doors” : 5 }”;

    byte[] bytes = carJson.getBytes(“UTF-8”);

    Car car = objectMapper.readValue(bytes, Car.class);

    ``
    可以讲domain转成HashMap
      ObjectMapper oMapper = new ObjectMapper();
       Map clerkMap = oMapper.convertValue(clerk, HashMap.class);
    
    展开全文
  • ObjectMapper用法

    2019-09-14 22:41:14
    private static ObjectMapper objectMapper = new ObjectMapper(); static { //config objectMapper.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);//设置在反序列化时忽略在JSON字符串...

    首先在pom.xml引入

    <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.8.3</version>
            </dependency>

     

    定义JsonMapper工具类

    public class JsonMapper {
    
        private static ObjectMapper objectMapper = new ObjectMapper();
    
        static {
            //config
            objectMapper.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);//设置在反序列化时忽略在JSON字符串中存在,而在Java中不存在的属性
            objectMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);//如果是空对象的时候,不抛异常
            objectMapper.setFilters(new SimpleFilterProvider().setFailOnUnknownId(false));
            objectMapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_EMPTY);//属性为null的转换
            objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        }
    
    
        /**
         * 对象转String
         * @param src
         * @param <T>
         * @return
         */
        public static <T> String obj2String(T src){
            if (null == src){
                return null;
            }
            try {
                return src instanceof String ? (String) src:objectMapper.writeValueAsString(src);
            } catch (Exception e) {
                return null;
            }
        }
    
        public static <T> T string2Obj(String src, TypeReference<T> typeReference){
            if(null == src || null == typeReference){
                return null;
            }
            try {
                return (T) (typeReference.getType().equals(String.class) ? src : objectMapper.readValue(src, typeReference));
            } catch (Exception e){
                return null;
            }
        }
     }

    测试类:

    public static void main(String[] args) {
            SysAcl acl = SysAcl.builder().id(1).name("测试").remark("ceshi").build();
            String a = JsonMapper.obj2String(acl);
            System.out.println(a);
            SysAcl sysAcl = JsonMapper.string2Obj(a, new TypeReference<SysAcl>() {});
            System.out.println(sysAcl.toString());
        }

     

    展开全文
  • ObjectMapper 动态用法

    2019-04-30 14:08:00
     ObjectMapper objectMapper = new ObjectMapper();  Object o = objectMapper.readValue(body, Object.class);  Object value = new DymicObject(o).get("Records").get(0).get("s3").get("bucket").get(...
  • ObjectMapper用法详解

    2019-08-24 23:04:42
    1、https://blog.csdn.net/qq_32454537/article/details/80672191 2、https://www.cnblogs.com/xuwenjin/p/8976696.html
  • ObjectMapper的基本用法

    2019-09-07 21:18:27
    ObjectMapper objectMapper = new ObjectMapper(); Product product = new Product("0001", "name", 1); objectMapper.writeValue(new File("target/product.json"), product); 将 Json String 赋值给 一个类 ...
  • Jackson中objectMapper的特殊用法

    千次阅读 2018-12-25 17:49:18
    private static final ObjectMapper objectMapper = new Jackson2ObjectMapperBuilder() .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).build(); private Class<T> valueType; /** * @...
  • Jackson的ObjectMapper的简单用法及语法

    千次阅读 2019-02-18 16:00:06
    1.使用ObjectMapper要添加的依赖是 &amp;amp;amp;lt;!--添加fastjson包--&amp;amp;amp;gt; &amp;amp;amp;lt;dependency&amp;amp;amp;gt; &amp;amp;amp;lt;groupId&amp;amp;amp;gt;...
  • 用法 创建一个实现Mappable协议的Class或Struct 。 import Foundation import ObjectMapper // MARK: Initializer and Properties struct Repository : Mappable { var identifier: Int ! var language: String ?...
  • Jackson(ObjectMapper) 在SCALA中的用法 maven配置: &lt;properties&gt; &lt;fasterxml.jackson.version&gt;2.6.5&lt;/fasterxml.jackson.version&gt; &lt;/properties&gt; ...
  • JAVA中ObjectMapper

    2021-01-13 16:08:02
    ObjectMapper是一个java内部类,可以在字符串和对象之间切换,下面是一些用法; 导入对应依赖 import com.fasterxml.jackson.databind.ObjectMapper; @Resource private ObjectMapper objectMapper; 由字符串转对象...
  • ObjectMapper框架的探索学习

    千次阅读 2016-10-09 23:34:54
    ObjectMapper框架的探索...Alamofire框架的用法这里不再介绍,请参考这里 ObjectMapper主要用于JSON数据和Model之间的相互转换的,类似框架还有OC中的MJExtension、jsonmodel等。 此处先给出服务器返回的json数据 {
  • 本文主要简单的介绍了Jackson中的一个重要类ObjectMapper用法。曾经以为这个类很难,但是真正操作了一遍之后发现,这个类的使用非常的简单。 简单使用的化要点不过就两个: 1 两个方法: 1.1 对象--&gt;...
  • spring boot2 (30)-Jackson和ObjectMapper

    万次阅读 2018-05-27 14:58:23
    在spring boot中,默认使用Jackson来实现java对象到json格式的序列化...ObjectMapper基本用法Jackson的转换是通过ObjectMapper对象来实现的,spring boot内部自动配置了一个ObjectMapper对象,我们可以直接用。 @Aut...
  • 用法 给定一个URL,该URL以以下形式返回天气数据: { "location": "Toronto, Canada", "three_day_forecast": [ { "conditions": "Partly cloudy", "day" : "Monday", "temperature": 20 }, { ...
  • JackJSON的用法

    2020-07-27 09:11:56
    网上大多数都是FastJSON的用法,很少有JackJSON的用法。我总结了几个自己经常用到的,供大家参考。 1.Java对象转化成JSON String getJSON(Object obj){ ObjectMapper objectMapper = new ObjectMapper(); //创建...
  • Jackson的用法

    2020-03-29 17:18:46
    Jackson的用法 Jackson对对象、List、Map、数组等详细使用方法 public class JacksonUtil { public static void main(String[] args) throws IOException { ObjectMapper objectMapper = new ObjectMapper(); //...
  • Jackson最简单用法

    2019-09-16 01:04:33
    jackson的包在:https://yunpan.cn/cu2b6eMaBjFpz 访问密码 753f 代码: ObjectMapper objectMapper = new ObjectMapper(); String jsonStr=objectMapper.writeVa...
  • jackson用法

    2017-02-03 11:28:00
    ObjectMapper mapper=new ObjectMapper(); //3.调用mapper的writeValueAsString()方法把一个对象或集合转为json字符串 Customer customer=new Customer("aaa", "333"); String jsonStr=mapper.writeValueAsString...
  • Jackson用法

    2020-06-30 17:19:05
    三、 ObjectMapper 一)、从JSON中获取Java对象 1、简单示例 2、 ObjectMapper如何匹配JSON对象的字段和Java对象的属性 3、JSON字符串–>Java对象 3、JSON 字符输入流–>Java对象 4、JSON文件–>Java对象 5...
  • Jackson 之用法

    2020-11-09 23:33:22
    目录Jackson 之用发Jackson ObjectMapper Example从Reader读取对象从File中读取对象从URL中读取对象从InputStream读取对象从字节数组中读取对象从JSON数组字符串中读取对象数组从JSON数组字符串中读取对象列表从JSON...
  • json用法

    2013-04-08 16:32:55
    近日,使用Jackson转化JSON对象的时候,显示的时候,日期始终显示不正确,输出的日期是一串数字代表的时间戳,不符合要求,所以想到Jackson应当有方法设置输出的日期格式。...objectMapper.configure(S
  • 本文就来针对Jackson的用法做一个较为详细的实例分析。 推荐使用:ObjectMapper提供的readTree,返回JsonNode对象,使用简单。 ObjectMapper mapper = new ObjectMapper(); String message="{\"code\":\"EX...
  • Java typereference用法

    千次阅读 2015-04-23 22:22:43
    jackson用来将java对象序列化和反序列化时,主要通过对象objectMapper。   refurl:http://kuyur.info/blog/archives/2782   http://www.cnblogs.com/pelephone/archive/2011/11/01/jackon-json-2-pojo-vo.html...
  • JSON工具类的用法

    2019-02-19 10:31:16
    package com.leyou.common.utils; import com.fasterxml.jackson.core.JsonProcessingException; import ... import com.fasterxml.jackson.databind.ObjectMapper; im...

空空如也

空空如也

1 2 3
收藏数 54
精华内容 21
关键字:

objectmapper用法