栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JSON转换工具类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JSON转换工具类

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import org.apache.commons.lang3.StringUtils;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;


public class JsonUtils {
    
    private static final String DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";

    private static ObjectMapper defaultObjectMapper = getObjectMapper(null);

    
    public static ObjectMapper getObjectMapper(String datePattern) {
        if (StringUtils.isBlank(datePattern)) {
            datePattern = DATE_PATTERN;
        }
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat(datePattern));
        //允许字段名字不使用引号
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
        //允许单引号
        objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        // 忽略未识别的参数(解决json串中多属性,而类中又未定义的情况)
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        //将对象转换成string时,json的key是否按照对象的field的自然顺序排列
        objectMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, false);
        return objectMapper;
    }

    
    public static String writeValueAsString(Object obj){
        try {
            return getObjectMapper(null).writeValueAsString(obj);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    
    public static String writeValueAsString(Object object, String datePattern) {
        try {
            return getObjectMapper(datePattern).writeValueAsString(object);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    
    public static String writeValueAsString(Object object, String datePattern,boolean indentOutput) {
        try {
            ObjectMapper objectMapper = getObjectMapper(datePattern);
            objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
            return objectMapper.writeValueAsString(object);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    
    public static  T readValue(String content, Class valueType) {
        try {
            ObjectMapper objectMapper = getObjectMapper(null);
            return objectMapper.readValue(content, valueType);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    
    public static  T readValue(String content, TypeReference valueTypeRef){
        try {
            ObjectMapper objectMapper = getObjectMapper(null);
            return objectMapper.readValue(content, valueTypeRef);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    
    public static  T readValue(String content, TypeReference valueTypeRef, String dateStyle){
        try {
            ObjectMapper objectMapper = getObjectMapper(dateStyle);
            return objectMapper.readValue(content, valueTypeRef);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    
    public static  T readValue(String content, Class valueType, String dateStyle) {
        try {
            ObjectMapper objectMapper = getObjectMapper(dateStyle);
            return objectMapper.readValue(content, valueType);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    
    public static JsonNode readTree(String content){
        try {
            ObjectMapper objectMapper = getObjectMapper(null);
            return objectMapper.readTree(content);
        }catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    
    public static JsonNode getJsonNode(JsonNode jsonNode , String path){
        String[] pathArr = path.split("\.");
        JsonNode temp = jsonNode;
        for(String filed : pathArr){
            temp = temp.get(filed);
        }
        return temp;
    }

    
    public static JsonNode getJsonNode(String json , String path){
        JsonNode root = readTree(json);
        return getJsonNode(root,path);
    }

    
    public static String getFieldValue(JsonNode jsonNode,String path){
        JsonNode node = getJsonNode(jsonNode,path);
        if(node==null){
            return null;
        }
        return node.asText();
    }

    public static String getFieldValue(String json , String path){
        JsonNode jsonNode = readTree(json);
        return getFieldValue(jsonNode,path);
    }

    public static void main(String[] args){
//		Map map = new HashMap();
//		map.put("name", "张三");
//		map.put("age", 50);
//		map.put("生日", new Date());
//		System.out.println(writeValueAsString(map));
//		System.out.println(writeValueAsString(map,"yyyy年MM月dd日HH时mm分"));

        String str = "{'username':'admin',password:'000000'}";

//		String str = "{ "name" : "萧远山", "sex" : "男", "age" : "23","address" : "河南郑州"}";
        Map map = readValue(str,HashMap.class);
        System.out.println(map);
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/985274.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号