For example, the use of Objectmapper class in the Java Jackson Library _java

Source: Internet
Author: User
Tags object object static class

The Objectmapper class is the main class of the Jackson Library. It provides functionality to convert Java objects to match the JSON structure, and vice versa. It uses Jsonparser and Jsongenerator instances to implement JSON's actual read/write.

class declaration
the following is a declaration of the Org.codehaus.jackson.map.ObjectMapper class:

public class Objectmapper
  extends Objectcodec
   implements versioned

Nested classes

S.N. Class & Description
1 Static class objectmapper.defaulttyperesolverbuilder//Custom Typeresolverbuilder, providing the so-called "default input" Use the type resolution builder (see Enabledefaulttyping () for more information).
2 The static class objectmapper.defaulttyping//uses the Enabledefaulttyping () enumeration to specify what type (Class) default input should be used.

Constructors
S.N. Constructors & Descriptions
1 Objectmapper ()//default constructor, which will build the default jsonfactory use Stdserializerprovider as its serializerprovider, if necessary. and Beanserializerfactory as its serializerfactory.
2 The Objectmapper (jsonfactory JF)//construct constructs the necessary jsonparsers and/or jsongenerators mappings using the specified jsonfactory.
3 Objectmapper (jsonfactory JF, Serializerprovider SP, deserializerprovider DP)
4 Objectmapper (jsonfactory JF, Serializerprovider SP, Deserializerprovider dp, Serializationconfig sconfig, Deserializationconfig dconfig)
5 Objectmapper (serializerfactory SF) is not recommended for use. Use other constructs to replace; Note that you can set the serial chemical Plant setserializerfactory (org.codehaus.jackson.map.SerializerFactory)

This class inherits the following class methods:
Java.lang.Object

Example
The test class Basic code is as follows

* * @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 (Convert entity class to JSON) * @param obj ECT * @throws IOException */public static void Writeentity2json (object) throws IOException {Ma 
      Pper.writevalue (New File ("D:\\developsoft\\aaadowload\\testjson1\\lib\\aa.txt"), object); 
      
   Mapper.writevalue (System.out,object); 
   /** * * <code>writeArray2Json</code> * @description: TODO (array converted to JSON array) * @param object * @throws IoexcEption */public static void Writearray2json (object) throws IOException {//WriteValue has and writ 
     Eobject the same function mapper.writevalue (new File ("D:\\developsoft\\aaadowload\\testjson1\\lib\\aa.txt"), object); 
      
   Mapper.writevalue (System.out,object); 
   /** * * <code>writeMap2Json</code> * @description: TODO (Map object converted to JSON object) * @param object * @throws IOException * @since 2011-11-8 Liao Yiping * * public static void Writemap2json (Object object) throws 
     IOException {System.out.println ("use Objectmapper-----------"); 
     WriteValue has the same function System.out.println as writeobject ("==>" +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 converted to JSON) * @param object * @Throws IOException */public static void Writelist2json (Object object) throws IOException {System.out.print 
     ln ("==>" +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 converted to entity) * @throws ioexcept Ion */public static void Writejson2entity () throws IOException {System.out.println ("JSON string converted to 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 ()); 
   
     Beautiful output//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 special list object) * @throws IOEXCEP tion */public static void Writejson2list () throws IOException {System.out.println ("JSON string converted to 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 is converted to list object/public static void Readjson2list () {String json = "[{\ uid\": 1,\ "un Ame\ ": \ 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)); 
    A catch (Jsonparseexception e) {e.printstacktrace ()); 
    catch (Jsonmappingexception e) {e.printstacktrace (); 
    catch (IOException e) {e.printstacktrace (); }/** * JSON is converted to list object/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 (); 

 } 
   } 
 
}

Print results:

string conversion into 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}}

Try it on your own, it's my test code.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.