JsonUtils
Integrate some operations to JSON:
package com.evan.common.utils;
Import java.util.list;
Import com.fasterxml.jackson.core.jsonProcessingException;
Import com.fasterxml.jackson.databind.javatype;
Import com.fasterxml.jackson.dataBind.objectMapper;
/**
* Taotao Mall customized response structure
*/
public class jsonutill {
// Define the Jackson object
Private Static Final ObjectMapper Mapper = New ObjectMapper ();
/**
* Convert the object to the JSON string.
* <p> Title: Pojotojson </p>
* <p> description: </p>
* @param Data
* @Return
*/
Public Static String Objecttojson (Object Data) {{
try {
String string = Mapper.writevalaSstring (data);
Return string;
} Catch (jsonprocessingException E) {{
e.printstacktrace ();
}
Return null;
}
/**
* Translate the JSON results set into objects
*
* @param jsondata json data
* @param Clazz Object type
* @Return
*/
Public Static <t> T JSONTOPOJO (String JSONDATA, Class <T> Beatype) {
try {
T = Mapper.readvalue (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 Beatype
* @Return
*/
Public Static <t> List <t> JSONTOLIS
Javatype javatype = Mapper.gettypefactory (). Constructor
try {
List <t> list = mapper.readvalue (jsondata, javatype);
Return list;
} Catch (Exception E) {
e.printstacktrace ();
}
Return null;
}
}