JSON string with the object, List, map of the Mutual-transfer tool class

Source: Internet
Author: User
Tags tojson

Tag:json    object    map   list   

Package Com.cq2022.zago.base.util;import Java.io.filereader;import Java.io.filewriter;import java.io.IOException; Import Java.io.stringwriter;import java.util.list;import java.util.map;import org.codehaus.jackson.JsonFactory; Import Org.codehaus.jackson.jsongenerationexception;import Org.codehaus.jackson.jsongenerator;import Org.codehaus.jackson.jsonnode;import Org.codehaus.jackson.jsonparseexception;import Org.codehaus.jackson.map.jsonmappingexception;import Org.codehaus.jackson.map.objectmapper;import Org.codehaus.jackson.map.serializationconfig;import Org.codehaus.jackson.map.annotate.jsonserialize;import com.alibaba.fastjson.jsonarray;/** * JSON tool class, implementing JSON and Java Bean conversion * user:shijing * <span style= "font-family:arial, Helvetica, Sans-serif; " >2015 April 3 Morning 10:42:19</span> */public class Jsonutils {private static objectmapper Objectmapper = new Objectmapper ();p rivate static jsonfactory jsonfactory = new Jsonfactory (); static {Objectmapper.configure ( Serializationconfig.feature. Write_null_map_values, False); Objectmapper.setserializationinclusion (JsonSerialize.Inclusion.NON_NULL);} /** * Generic return, JSON string to Object * April 3, 2015 morning 10:42:19 * auther:shijing * @param jsonasstring * @param pojoclass * @return * @throws Jsonmappingexception * @throws jsonparseexception * @throws ioexception */public static <T> T Fromjson (String Jsonas String, class<t> Pojoclass) throws Jsonmappingexception,jsonparseexception, IOException {return Objectmapper.readvalue (jsonasstring, pojoclass);} public static <T> T Fromjson (FileReader fr, class<t> Pojoclass) throws Jsonparseexception, IOException {retur N Objectmapper.readvalue (FR, pojoclass);} /** * object to JSON * April 3, 2015 morning 10:41:53 * auther:shijing * @param pojo * @return * @throws jsonmappingexception * @throw S jsongenerationexception * @throws ioexception */public static String ToJson (Object pojo) throws Jsonmappingexception, Js Ongenerationexception, IOException {return ToJson (Pojo, false);} public static String ToJson (ObJect Pojo, Boolean prettyprint) throws Jsonmappingexception, jsongenerationexception,ioexception {stringwriter SW = new S Tringwriter (); Jsongenerator JG = Jsonfactory.createjsongenerator (SW); if (prettyprint) {jg.usedefaultprettyprinter ();} Objectmapper.writevalue (JG, Pojo); return sw.tostring ();} public static void ToJson (Object Pojo, FileWriter FW, Boolean Prettyprint) throws Jsonmappingexception, Jsongenerationexception, IOException {jsongenerator JG = Jsonfactory.createjsongenerator (FW), if (PrettyPrint) { Jg.usedefaultprettyprinter ();} Objectmapper.writevalue (JG, Pojo);} /** * JSON string to map * April 3, 2015 morning 10:41:25 * auther:shijing * @param jsonstr * @return * @throws ioexception */public static Map<string, object> parsemap (String jsonstr) throws IOException {map<string, object> Map = Objectmapper.readvalue (Jsonstr, Map.class); return Map;} public static Jsonnode Parse (String jsonstr) throws IOException {Jsonnode node = Null;node = Objectmapper.readtree (jsonstr ); return node;} Public STATic Objectmapper Getobjectmapper () {return objectmapper;} /** * JSON string to list Object * April 2, 2015 morning 10:22:20 * auther:shijing * @param strjson String * @param clazz conversion type * @return */public St atic <T> list<t> Fromlistjson (String str,class<t> clazz) {return Jsonarray.parsearray (str, clazz);}}

JSON string with the object, List, map of the Mutual-transfer tool class

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.