Json tool encapsulation and parsing, json tool Encapsulation

Source: Internet
Author: User

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 ;}}

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.