当前位置: 移动技术网 > IT编程>开发语言>Java > 举例讲解Java的Jackson库中ObjectMapper类的使用

举例讲解Java的Jackson库中ObjectMapper类的使用

2019年07月22日  | 移动技术网IT编程  | 我要评论

培训中心章程,少女漫画在线,微笑的闪士下载

objectmapper类是jackson库的主要类。它提供一些功能将转换成java对象匹配json结构,反之亦然。它使用jsonparser和jsongenerator的实例实现json实际的读/写。

类声明
以下是org.codehaus.jackson.map.objectmapper类的声明:

public class objectmapper
  extends objectcodec
   implements versioned

嵌套类

s.n. 类 & 描述
1 static class objectmapper.defaulttyperesolverbuilder//定制typeresolverbuilder,提供所谓的“默认输入”使用类型解析构建器(见enabledefaulttyping()了解详细信息)。
2 static class objectmapper.defaulttyping//使用enabledefaulttyping()枚举指定什么样的类型(类)默认输入应该使用。

构造函数
s.n. 构造函数 & 描述
1 objectmapper()//默认的构造函数,这将构建默认jsonfactory必要时使用stdserializerprovider作为其serializerprovider,并beanserializerfactory作为其serializerfactory。
2 objectmapper(jsonfactory jf)//构造使用指定的jsonfactory构建必要的jsonparsers和/或jsongenerators映射。
3 objectmapper(jsonfactory jf, serializerprovider sp, deserializerprovider dp)
4 objectmapper(jsonfactory jf, serializerprovider sp, deserializerprovider dp, serializationconfig sconfig, deserializationconfig dconfig)
5 objectmapper(serializerfactory sf) 不推荐使用。使用其他构造来代替; 注意,可以设置序列化工厂setserializerfactory(org.codehaus.jackson.map.serializerfactory)

这个类继承了以下类方法:
java.lang.object

例子
测试类基本代码如下

/* 
 * @project java 
 * @package 
 * @file jackson.java 
 * @version 1.0 

 */ 
 
public class jackson { 
  /* 
   * 
   * class descripton goes here. 
   * 
   * @class jackson 
   * @version 1.0 
   */ 
  public static jsongenerator jsongenerator = null; 
  private static objectmapper mapper = new objectmapper(); 
  public static void main(string[] args) { 
    student student = new student(); 
    student.setisstudent(true); 
    student.setuid(1000); 
    student.setuname("xiao liao"); 
    student.setupwd("123"); 
    student.setnumber(12); 
     
    map<string, student> stumap = new hashmap<string, student>(); 
    stumap.put("1", student); 
    stumap.put("2", student); 
     
    list<object> stulist = new arraylist<object>(); 
    list<student> stulist1 = new arraylist<student>(); 
    stulist1.add(student); 
    student= new student(); 
    student.setisstudent(false); 
    student.setuid(200); 
    student.setuname("xiao mi"); 
    stulist1.add(student); 
     
    stulist.add(student); 
    stulist.add("xiao xin"); 
    stulist.add("xiao er"); 
    stulist.add(stumap); 
     
    //readjson2list(); 
    try { 
      //readjson2array(); 
      //writearray2json(array); 
      //writejson2list(); 
      //writeentity2json(student); 
      writejson2entity(); 
      //writemap2json(stumap); 
      //writelist2json(stulist1); 
       
    } catch (ioexception e) { 
      e.printstacktrace(); 
    } 
  } 
   /** 
   * 
   * <code>writeentity2json</code> 
   * @description: todo(实体类转换成json) 
   * @param object 
   * @throws ioexception 
   */ 
   public static void writeentity2json(object object) throws ioexception { 
      mapper.writevalue( new file("d:\\developsoft\\aaadowload\\testjson1\\lib\\aa.txt"),object ); 
      mapper.writevalue( system.out,object ); 
      
   } 
   /** 
   * 
   * <code>writearray2json</code> 
   * @description: todo(数组转换成json数组) 
   * @param object 
   * @throws ioexception 
   */ 
   public static void writearray2json(object object) throws ioexception { 
      
     // writevalue具有和writeobject相同的功能 
     mapper.writevalue( new file("d:\\developsoft\\aaadowload\\testjson1\\lib\\aa.txt"),object ); 
     mapper.writevalue(system.out,object ); 
      
   } 
   /** 
   * 
   * <code>writemap2json</code> 
   * @description: todo(map对象转换成json对象) 
   * @param object 
   * @throws ioexception 
   * @since  2011-11-8   廖益平 
   */ 
   public static void writemap2json(object object) throws ioexception { 
      
     system.out.println("使用objectmapper-----------"); 
     // writevalue具有和writeobject相同的功能 
     system.out.println("==>"+mapper.writevalueasstring(object)); 
     mapper.writevalue( new file("d:\\developsoft\\aaadowload\\testjson1\\lib\\aamap.txt"),object ); 
     mapper.writevalue( system.out , object ); 
   } 
   /** 
   * 
   * <code>writelist2json</code> 
   * @description: todo(list转换成json) 
   * @param object 
   * @throws ioexception 
   */ 
   public static void writelist2json(object object) throws ioexception { 
     system.out.println("==>"+mapper.writevalueasstring(object)); 
     mapper.writevalue( new file("d:\\developsoft\\aaadowload\\testjson1\\lib\\aamap.txt"),object ); 
     mapper.writevalue( system.out , object ); 
   } 
   /** 
   * 
   * <code>writejson2entity</code> 
   * @description: todo(json转换成实体) 
   * @throws ioexception 
   */ 
   public static void writejson2entity() throws ioexception { 
     system.out.println("json串转换成entity-------------"); 
//    file file = new file("d:\\developsoft\\aaadowload\\testjson1\\lib\\aa.txt"); 
//    fileinputstream inputstream = new fileinputstream(file); 
//    student student = mapper.readvalue(inputstream,student.class); 
//    system.out.println(student.tostring()); 
     //漂亮输出 
     //mapper.defaultprettyprintingwriter().writevalueasstring(value); 
   
     string json = "{\"uid\":1000,\"uname\":\"xiao liao\",\"upwd\":\"123\",\"number\":12.0,\"isstudent\":true}"; 
     student student1 = mapper.readvalue(json,student.class); 
     system.out.println("json2:"+student1.tostring()); 
   } 
   /** 
   * 
   * <code>writejson2list</code> 
   * @description: todo(json专程list对象) 
   * @throws ioexception 
   */ 
   public static void writejson2list() throws ioexception { 
     system.out.println("json串转换成entity-------------"); 
     file file = new file("d:\\developsoft\\aaadowload\\testjson1\\lib\\aa.txt"); 
     fileinputstream inputstream = new fileinputstream(file); 
     student student = mapper.readvalue(inputstream,student.class); 
     system.out.println(student.tostring()); 
      
     string json = "[{\"uid\":1000,\"uname\":\"xiao liao\",\"upwd\":\"123\",\"number\":12.0,\"isstudent\":true},{\"uid\":200,\"uname\":\"xiao mi\",\"upwd\":null,\"number\":0.0,\"isstudent\":false}]"; 
     list<linkedhashmap<string, object>> s= mapper.readvalue(json,list.class); 
     for (int i = 0; i < s.size(); i++) { 
       linkedhashmap<string, object> link = s.get(i); 
       set<string> key = link.keyset(); 
       for (iterator iterator = key.iterator(); iterator.hasnext();) { 
        string string = (string) iterator.next(); 
        system.out.println(string+"==>"+link.get(string)); 
         
      } 
       system.out.println("json:"+i+""+s.get(i).tostring()); 
       
    } 
   } 
   /** 
    * json转换为list对象 
    */ 
   public static void readjson2list() { 
    string json = "[{\"uid\":1,\"uname\":\"www\",\"number\":234,\"upwd\":\"456\"}," 
     + "{\"uid\":5,\"uname\":\"tom\",\"number\":3.44,\"upwd\":\"123\"}]"; 
    try { 
    list<linkedhashmap<string, object>> list = mapper.readvalue( 
     json, list.class); 
    system.out.println(list.size()); 
    for (int i = 0; i < list.size(); i++) { 
     map<string, object> map = list.get(i); 
     set<string> set = map.keyset(); 
     for (iterator<string> it = set.iterator(); it.hasnext();) { 
     string key = it.next(); 
     system.out.println(key + ":" + map.get(key)); 
     } 
    } 
    } catch (jsonparseexception e) { 
    e.printstacktrace(); 
    } catch (jsonmappingexception e) { 
    e.printstacktrace(); 
    } catch (ioexception e) { 
    e.printstacktrace(); 
    } 
   } 
   /** 
    * json转换为list对象 
    */ 
   public static void readjson2array() { 
     string json = "[{\"uid\":1,\"uname\":\"www\",\"number\":234,\"upwd\":\"456\"}," 
       + "{\"uid\":5,\"uname\":\"tom\",\"number\":3.44,\"upwd\":\"123\"}]"; 
     try { 
       student[] students = mapper.readvalue(json, student[].class); 
       for (student student : students) { 
        system.out.println(">"+student.tostring()); 
      } 
     } catch (jsonparseexception e) { 
       e.printstacktrace(); 
     } catch (jsonmappingexception e) { 
       e.printstacktrace(); 
     } catch (ioexception e) { 
       e.printstacktrace(); 
     } 
   } 
 
} 

打印结果 :

串转换成entity-------------
json2:uid=1000,name=xiao liao,upwd=123,number=12.0,isstudent=true

writemap2json -----------
{"2":{"uid":1000,"uname":"xiao liao","upwd":"123","number":12.0,"isstudent":true},"1":{"uid":1000,"uname":"xiao liao","upwd":"123","number":12.0,"isstudent":true}}

readjson2array------------------
>uid=1,name=www,upwd=456,number=234.0,isstudent=false
>uid=5,name=tom,upwd=123,number=3.44,isstudent=false
writemap2json -----------
{"2":{"uid":1000,"uname":"xiao liao","upwd":"123","number":12.0,"isstudent":true},"1":{"uid":1000,"uname":"xiao liao","upwd":"123","number":12.0,"isstudent":true}}

大家逐个自己试试吧  ,上面也是我的测试代码

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网