189 8069 5689

Json字符串与Object、List、Map的互转工具类

package com.cq2022.zago.base.util;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import java.util.Map;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import com.alibaba.fastjson.JSONArray;
/**
 * Json工具类,实现JSON与Java Bean的互相转换
 */
public class JsonUtils {
 private static ObjectMapper objectMapper = new ObjectMapper();
 private static JsonFactory jsonFactory = new JsonFactory();
 static {
 objectMapper.configure(SerializationConfig.Feature.WRITE_NULL_MAP_VALUES, false);
 objectMapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
 }
 /**
 * 泛型返回,json字符串转对象
 * @param jsonAsString
 * @param pojoClass
 * @return
 * @throws JsonMappingException
 * @throws JsonParseException
 * @throws IOException
 */
 public static  T fromJson(String jsonAsString, Class pojoClass) throws JsonMappingException,
  JsonParseException, IOException {
 return objectMapper.readValue(jsonAsString, pojoClass);
 }
 public static  T fromJson(FileReader fr, Class pojoClass) throws JsonParseException, IOException {
 return objectMapper.readValue(fr, pojoClass);
 }
 /**
 * Object对象转json
 * @param pojo
 * @return
 * @throws JsonMappingException
 * @throws JsonGenerationException
 * @throws IOException
 */
 public static String toJson(Object pojo) throws JsonMappingException, JsonGenerationException, IOException {
 return toJson(pojo, false);
 }
 public static String toJson(Object pojo, boolean prettyPrint) throws JsonMappingException, JsonGenerationException,
  IOException {
 StringWriter sw = new StringWriter();
 JsonGenerator jg = jsonFactory.createJsonGenerator(sw);
 if (prettyPrint) {
  jg.useDefaultPrettyPrinter();
 }
 objectMapper.writeValue(jg, pojo);
 return sw.toString();
 }
 public static void toJson(Object pojo, FileWriter fw, boolean prettyPrint) throws JsonMappingException,
  JsonGenerationException, IOException {
 JsonGenerator jg = jsonFactory.createJsonGenerator(fw);
 if (prettyPrint) {
  jg.useDefaultPrettyPrinter();
 }
 objectMapper.writeValue(jg, pojo);
 }
 /**
 * json字符串转Map
 * @param jsonStr
 * @return
 * @throws IOException
 */
 public static Map parseMap(String jsonStr) throws IOException {
 Map map = objectMapper.readValue(jsonStr, Map.class);
 return map;
 }
 public static JsonNode parse(String jsonStr) throws IOException {
 JsonNode node = null;
 node = objectMapper.readTree(jsonStr);
 return node;
 }
 public static ObjectMapper getObjectMapper() {
 return objectMapper;
 }
 /**
 * json字符串转 List对象
 * @param str json字符串
 * @param clazz 转换的类型
 * @return
 */
 public static  List fromListJson(String str,Class clazz){
 return JSONArray.parseArray(str, clazz);
 }
}

总结

我们提供的服务有:成都网站建设、网站设计、微信公众号开发、网站优化、网站认证、冀州ssl等。为千余家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的冀州网站制作公司

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对创新互联的支持。如果你想了解更多相关内容请查看下面相关链接


文章题目:Json字符串与Object、List、Map的互转工具类
URL链接:http://cdxtjz.cn/article/ihcdci.html

其他资讯