Use Jackson for JSON conversion in spring

Source: Internet
Author: User

Use Jackson for JSON conversion in spring

Import Java.util.list;import Com.fasterxml.jackson.core.jsonprocessingexception;import Com.fasterxml.jackson.databind.javatype;import Com.fasterxml.jackson.databind.jsonnode;import Com.fasterxml.jackson.databind.objectmapper;import Com.taotao.result.taotaoresult;public class JsonUtils {//    Define the Jackson object private static final Objectmapper MAPPER = new Objectmapper ();     /** * Converts an object to a JSON string. * <p>Title:pojoToJson</p> * <p>description: </p> * @param data * @return */PU Blic static string Objecttojson (Object data) {try {string string = mapper.writevalueasstring (data); return string;} cat    CH (jsonprocessingexception e) {e.printstacktrace ();}    return null;    /** * Convert JSON result set to Object * * @param jsondata JSON data * @param type of object in Clazz Object * @return * * * public static <T> T Jsontopojo (String jsondata, class<t> beantype) {try {t t = MAPPER.R      Eadvalue (Jsondata, Beantype);      return t;        } catch (Exception e) {e.printstacktrace ();    } return null;     /** * Convert JSON data to Pojo object list * <p>Title:jsonToList</p> * <p>description: </p>  * @param jsondata * @param beantype * @return */public static <T>List<T> Jsontolist (String Jsondata, class<t> beantype) {Javatype javatype = Mapper.gettypefactory (). Constructparametrictype (List.class, b    Eantype);    try {list<t> List = Mapper.readvalue (Jsondata, Javatype); return list;}        catch (Exception e) {e.printstacktrace ();}    return null; }    }

Using Jackson for JSON conversion in spring

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.