Json tool encapsulation and parsing, json tool Encapsulation
Package com. chinatelecom. personalcustom. common. util; import java. io. IOException; import com. alibaba. fastjson. JSON; import com. fasterxml. jackson. core. jsonParser; import com. fasterxml. jackson. databind. jsonNode; import com. fasterxml. jackson. databind. objectMapper; public class JsonUtil {/*** converts a String to an entity Class, and allows a slash or other strings */public static <T> T jsonToEntity (String json, class <T> clazz) throws IOException {ObjectMapper mapper = new ObjectMapper (); // allow er characters such as backslashes. configure (JsonParser. feature. ALLOW_UNQUOTED_CONTROL_CHARS, true); return mapper. readValue (json, clazz);}/*** convert Object class to JSON String */public static String entityToJson (Object entity) {return JSON. toJSONString (entity);}/*** convert the String to a JsonNode, and allow the slash and other strings */public static JsonNode jsonToJsonNode (String json) throws IOException {ObjectMapper mapper = new ObjectMapper (); // allow er characters such as backslashes. configure (JsonParser. feature. ALLOW_UNQUOTED_CONTROL_CHARS, true); // allow single quotes mapper. configure (JsonParser. feature. ALLOW_SINGLE_QUOTES, true); return mapper. readValue (json, JsonNode. class);} public static <T> String objectToJson (Object object, Class <T> cls) throws Exception {ObjectMapper mapper = new ObjectMapper (); mapper. registerSubtypes (cls); String reqJson = mapper. writeValueAsString (object); return reqJson ;}}