Core Library
Http://repo1.maven.org/maven2/com/fasterxml/jackson/core/
Jackson-annotations-2.2.2.jar
Jackson-core-2.2.2.jar
Jackson-databind-2.2.2.jar
File Type Support Module
Http://repo1.maven.org/maven2/com/fasterxml/jackson/dataformat/
Jackson-dataformat-xml-2.2.2.jar
Import to Warehouse
Import com. fasterxml. Jackson. databind. objectmapper;
Import com. fasterxml. Jackson. databind. jsonnode;
Import com. fasterxml. Jackson. databind. jsonmappingexception;
Import com. fasterxml. Jackson. databind. node. objectnode;
Import com. fasterxml. Jackson. databind. node. arraynode;
Import com. fasterxml. Jackson. Core. jsongenerator;
Import com. fasterxml. Jackson. Core. jsonencoding;
Import com. fasterxml. Jackson. Core. jsonparseexception;
/*** Map to JSON */public static void mytest01 () {Map <string, string> hashmap = new hashmap <string, string> (); hashmap. put ("name", "Zhang"); hashmap. put ("sex", "1"); hashmap. put ("login", "Jack"); hashmap. put ("password", "123abc"); try {objectmapper = new objectmapper (); string usermapjson = objectmapper. writevalueasstring (hashmap); jsonnode node = objectmapper. readtree (usermapjson); // The output result is converted and the correct information is output. out. println (node. get ("password "). astext (); // The output does not translate. The output result will contain "". This is incorrect unless it is passed as JSON. If it is an output value, the system must be operated on the above line. out. println (node. get ("name");} catch (ioexception e ){}}
/*** Parse JSON format string */public static void mytest03 () {try {string STR = "{\" data \ ":{\" birth_day \ ": 7, \ "birth_month \": 6 },\ "errcode \": 0, \ "MSG \": \ "OK \", \ "RET \": 0 }"; objectmapper mapper = new objectmapper (); jsonnode root = mapper. readtree (STR); jsonnode DATA = root. PATH ("data"); jsonnode birth_day = data. PATH ("birth_day"); system. out. println (birth_day.asint (); jsonnode birth_month = data. PATH ("birth_month"); system. out. println (birth_month.asint (); jsonnode MSG = root. PATH ("MSG"); system. out. println (MSG. textvalue ();} catch (ioexception e ){}}
/*** JSON extract value directly */public static void mytest05 () {try {// demo string STR = "{\" data \": {\ "hasnext \": 0, \ "info \": [{\ "ID \": \ "288206077664983 \", \ "timestamp \": 1371052476 }, {\ "ID \": \ "186983078111768 \", \ "timestamp \": 1370944068 },{ \ "ID \": \ "297031120529307 \", \ "timestamp \": 1370751789 },{ \ "ID \": \ "273831022294863 \", \ "timestamp \": 1369994812}], \ "timestamp \": 1374562897, \ "totalnum \": 422}, \ "errcode \": 0, \ "MSG \": \ "OK \", \ "RET \": 0, \ "seqid \": 5903702688915195270} "; objectmapper mapper = new objectmapper (); jsonnode root = mapper. readtree (STR); // extract data jsonnode DATA = root. PATH ("data"); // extract info jsonnode info = data. PATH ("info"); system. out. println (info. size (); // obtain the 0th jsonnode items of info = info. get (0); system. out. println (item. get ("ID"); system. out. println (item. get ("timestamp"); // get the 2nd items of info = info. get (2); system. out. println (item. get ("ID"); system. out. println (item. get ("timestamp"); // traverses the array if (info. isarray () {for (jsonnode objnode: INFO) {system. out. println (objnode) ;}} catch (exception e ){}}
/*** Create a JSON file and add content to the JSON file */public static void mytest07 () {try {objectmapper mapper = new objectmapper (); objectnode root1 = mapper. createobjectnode (); root1.put ("nodekey1", 1); root1.put ("nodekey2", 2); system. out. println (root1.tostring (); // create the root node objectnode root = mapper. createobjectnode (); // create a child node objectnode node1 = mapper. createobjectnode (); node1.put ("nodekey1", 1); node1.put ("nodekey2", 2); // bind the child nodes root. put ("child", node1); // array of nodes arraynode = mapper. createarraynode (); arraynode. add (node1); arraynode. add (1); // bind array node root. put ("arraynode", arraynode); system. out. println (mapper. writevalueasstring (Root);} catch (exception e ){}}
References
Http://wiki.fasterxml.com/JacksonInFiveMinutes