Jackson's JSON processing-objectmapper

Source: Internet
Author: User

Jackson can easily convert Java objects into JSON objects and XML documents, as well as convert JSON, XML to Java objects 1, download the dependency library jar package Jackson jar    All Jackson-all-1.7.6.jar because the following program runs with JUnit test cases, you also have to add JUnit jar packages If you need to convert XML, then you need to STAX2-API.JAR2, test the basic code of the class as follows
public class Jackson {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 ();} } public static void Writeentity2json (Object object) throws IOException {Mapper.writevalue ("d:\\t          Estjson1\\lib\\aa.txt "), object);       Mapper.writevalue (System.out,object);           public static void Writearray2json (Object object) throws IOException {//WriteValue has the same functionality as WriteObject           Mapper.writevalue (New File ("D:\\testjson1\\lib\\aa.txt"), object);           Mapper.writevalue (System.out,object); public static void Writemap2json (Object object) throws IOException {SystEm.out.println ("Use of Objectmapper-----------");           WriteValue has the same function as WriteObject System.out.println ("==>" +mapper.writevalueasstring (object));           Mapper.writevalue (New File ("D:\\testjson1\\lib\\aamap.txt"), object);       Mapper.writevalue (System.out, object); } public static void Writelist2json (Object object) throws IOException {System.out.println ("==>" +mapper           . Writevalueasstring (object));           Mapper.writevalue (New File ("D:\\testjson1\\lib\\aamap.txt"), object);       Mapper.writevalue (System.out, object); } public static void Writejson2entity () throws IOException {System.out.println ("JSON string converted to entity----------  ---");  File File = new file ("D:\\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 ()); } public static void Writejson2list () throws IOException {System.out.println ("JSON string converted to entity-------------           ");           File File = new file ("D:\\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 converted to a list object 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 converted to a list object public static void Readjson2array () {String json = "[{\" uid\ ": 1,\" uname\ ": \" ww W\ ", \" 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 Result: string converted to entity-------------json2:uid=1000, Name=xiao Liao,upwd=123,number=12.0,isstudent=truewritemap2json-----------{"2": {"UID": +, "uname": "Xiao Liao", " Upwd ":" 123 "," number ": 12.0," Isstudent ": true},  
" 1 ": {" UID ": +," 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=falsewritemap2json--------- --{"2": {"UID": +, "uname": "Xiao Liao", "upwd": "123", "number": 12.0, "Isstudent": true},
public class Student {        private int uid;        Private String uname;        Private String upwd;        private double number;        Private Boolean isstudent;       

Jackson's JSON processing-objectmapper

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.