Jackson High-Performance JSON processing required for android/mobile development jackson.

Source: Internet
Author: User
I have studied json on my own today and it feels very useful. After testing, it is much faster than google's GSON. At the same time, Jackson can easily convert Java objects into json objects and xml documents, you can also convert json and xml into Java objects. Powerful functions! Everybody knows... syntaxHighlighter. all (); I have studied json on my own today and it feels very useful. After testing, it is much faster than google's GSON. At the same time, Jackson can easily convert Java objects into json objects and xml documents, you can also convert json and xml into Java objects. Powerful functions! As we all know, json is widely used in today's Internet era. Due to your attention, json resolution performance requirements are extremely high. 1. Preparation 1. Download the jar package of the dependent library Jackson and import the jar package into the project to start working. Because the following program runs with the junit test case, therefore, you must add the jar package of junit. Version is junit-4.2.8 if you need to convert xml, then also need stax2-api.jar2, test class basic code as follows [java]/** @ project java * @ package * @ file Jackson. java * @ version 1.0 * @ author Liao Yiping * @ time 2011-11-8 02:59:37 */public class Jackson {/*** Class Descripton goes here. ** @ class Jackson * @ version 1.0 * @ author Liao Yiping * @ time 2011-11-8 02:59:37 */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 StuMap = new HashMap (); StuMap. put ("1", student); stuMap. put ("2", student); List StuList = new ArrayList(); List StuList1 = new ArrayList (); 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 ();}}/**** writeEntity2Json* @ Description: TODO (convert object class to json) * @ param Object * @ throws IOException * @ since 2011-11-8 Liao Yiping */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 );}/**** writeArray2Json* @ Description: TODO (array converted to json array) * @ param object * @ throws IOException * @ since 2011-11-8 Liao Yiping */public static void writeArray2Json (Object object) throws IOException {// writeValue has the same functions as writeObject mapper. writeValue (new File ("D: \ developSoft \ aaadowload \ testjson1 \ lib \ aa.txt"), object); mapper. writeValue (System. out, object );}/**** writeMap2Json* @ Description: TODO (map object converted to json Object) * @ param object * @ throws IOException * @ since 2011-11-8 Liao Yiping */public static void writeMap2Json (object Object object) throws IOException {System. out. println ("use ObjectMapper -----------"); // writeValue has the same functions as writeObject. out. println ("=>" + mapper. writeValueAsString (object); mapper. writeValue (new File ("D :\\ developSoft \ aaadowload \ testjson1 \ lib \ aamap.txt"), object); mapper. writeValue (System. out, object );}/**** writeList2Json* @ Description: TODO (convert list to json) * @ param object * @ throws IOException * @ since 2011-11-8 Liao Yiping */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 );}/**** writeJson2Entity* @ Description: TODO (json converted to an object) * @ throws IOException * @ since 2011-11-8 Liao Yiping */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 ());}/**** writeJson2List* @ Description: TODO (json special list object) * @ throws IOException * @ since 2011-11-8 Liao Yiping */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 > S = mapper. readValue (json, List. class); for (int I = 0; I <s. size (); I ++) {LinkedHashMap Link = s. get (I); Set 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 () ;}}/*** convert JSON to 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 > List = mapper. readValue (json, List. class); System. out. println (list. size (); for (int I = 0; I <list. size (); I ++) {Map Map = list. get (I); Set Set = map. keySet (); for (Iterator 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 () ;}/ *** convert JSON 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 result: String converted to entity ------------- json2: uid = 1000, name = xiao liao, upwd = 123, number = 12.0, isStudent = truewriteMap2Json ----------- {"2": {"uid": 1000, "uname": "xiao", "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 = falsewriteMap2Json ----------- {"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 one by one. The above is also my test code [java] entity class: /** @ project java * @ package * @ file Student. java * @ version 1.0 * @ author Liao Yiping * @ time 2011-11-8 03:01:08 */public class Student {/*** Class Descripton goes here. ** @ class Student * @ version 1.0 * @ author Liao Yiping * @ time 2011-11-8 03:01:08 */private int uid; private String uname; private String upwd; private double number; private boolean isstudent; public int getUid () {return uid;} public void setUid (int uid) {this. uid = uid;} public String getUname () {return uname;} public void setUname (String uname) {this. uname = uname;} public String getUpwd () {return upwd;} public void setUpwd (String upwd) {this. upwd = upwd;} public double getNumber () {return number;} public void setNumber (double number) {this. number = number;} public boolean isIsstudent () {return isstudent;} public void setIsstudent (boolean isstudent) {this. isstudent = isstudent;} @ Override public String toString () {return "uid =" + uid + ", name =" + uname + ", upwd =" + upwd + ", number = "+ number +", isStudent = "+ isstudent ;}}
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.