json封装与解析的Java工具类

n24d的头像 n24d 10 2015-01-04 17:15 1

 基本信息

× 1   

浏览数: 7932

分享时间: 4 年 前

9
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 {  
      
    /** 
     * 将字符串转成实体类,允许斜杠等字符串 
     */  
    public static <T> T jsonToEntity(String json, Class<T> clazz) throws IOException {  
        ObjectMapper mapper = new ObjectMapper();  
        // 允许反斜杆等字符  
        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS,true);  
        return mapper.readValue(json, clazz);  
    }  
      
    /** 
     * 实体类转JSON字符串 
     */  
    public static String entityToJson(Object entity){  
        return JSON.toJSONString(entity);  
    }  
      
    /** 
     * 将字符串转成JsonNode,允许斜杠等字符串 
     */  
    public static JsonNode jsonToJsonNode(String json) throws IOException {  
        ObjectMapper mapper = new ObjectMapper();  
        // 允许反斜杆等字符  
        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS,true);  
        //允许单引号  
        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;  
    }  
      
}  


12 3 [下一页]

  • ossaa的头像 ossaa 2019-02-16 04:53 代码数:0

    I really thank you for the valuable info on this great subject and look forward to more great posts. Thanks a lot for enjoying this beauty article with me. I am appreciating it very much! Looking forward to another great article. Good luck to the author! All the best!

    Gavazzi

  • pevahanifo的头像 pevahanifo 2018-08-28 22:35 代码数:0

    I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. Thanks...forrentpensacola.com

  • pevahanifo的头像 pevahanifo 2018-08-30 02:22 代码数:0

    Thanks for your insight for your fantastic posting. I’m glad I have taken the time to see this.Furnace service Edmonton

  • jhoneila的头像 jhoneila 2018-12-22 16:13 代码数:0

    I recently came across your blog and have been reading along. I thought I would leave my first comment. I don’t know what to say except that I have enjoyed reading. Nice blog, I will keep visiting this blog very often. CPR Training Tucson

  • cixexey的头像 cixexey 2018-11-22 18:22 代码数:0

    this is really nice to read..informative post is very good to read..thanks a lot! new technologies

  • cixexey的头像 cixexey 2018-10-02 14:38 代码数:0

    Your website is really cool and this is a great inspiring article.Your website is really cool and this is a great inspiring article. jui residence location

  • lgp_open的头像 lgp_open 2017-06-08 23:14 代码数:0

    需要哪个jar包啊

  • cixexey的头像 cixexey 2018-10-03 17:54 代码数:0

    This blog website is pretty cool! How was it made !Prisluskivaci

  • 一步一世界的头像 一步一世界 2017-06-12 21:10 代码数:0

    赞一个!

  • cixexey的头像 cixexey 2018-10-04 22:31 代码数:0

    this is really nice to read..informative post is very good to read..thanks a lot!The Woodleigh residence showflat

您的评论: