zoukankan      html  css  js  c++  java
  • 对象转json串


    public static Object returnObject(String jsonStr, Class objClass) {
    if (jsonStr == null) {
    return null;
    }
    Object obj = null;
    try {
    obj = mapper.readValue(jsonStr, objClass);
    } catch (JsonParseException e) {
    logger.info(logger.getName() + "-->returnObject exception:" + e);
    } catch (JsonMappingException e) {
    logger.info(logger.getName() + "-->returnObject exception:" + e);
    } catch (IOException e) {
    logger.info(logger.getName() + "-->returnObject exception:" + e);
    } finally {
    return obj;
    }
    }


    public static JsonNode returnJsonNode(String jsonString) {
    JsonNode jsonNode = null;
    try {
    jsonNode = mapper.readTree(jsonString);
    } catch (IOException e) {
    logger.error("error in returnJsonNode due to {}" + e);
    }
    return jsonNode;
    }


    public static<T> T returnType(String jsonStr, TypeReference<T> valueTypeRef) {
    if (jsonStr == null) {
    return null;
    }
    T t = null;
    try {
    t = mapper.readValue(jsonStr, valueTypeRef);
    } catch (JsonParseException e) {
    logger.info(logger.getName() + "-->returnType:" + e);
    } catch (JsonMappingException e) {
    logger.info(logger.getName() + "-->returnType:" + e);
    } catch (IOException e) {
    logger.info(logger.getName() + "-->returnType:" + e);
    } finally {
    return t;
    }
    }

    public static <T> T readValue(JsonNode rootJsonNode, List<String> fieldNames, Class<T> valueType) {

    JsonNode jsonNode = getJsonNode(rootJsonNode, fieldNames);

    if (jsonNode == null || valueType == null) {
    return null;
    }

    try {
    return mapper.readerFor(valueType).readValue(jsonNode);
    } catch (IOException e) {
    logger.info(logger.getName() + "-->readValue:" + e.getMessage());
    }

    return null;
    }


    public static JsonNode getJsonNode(JsonNode rootJsonNode, List<String> fieldNames) {
    JsonNode jsonNode = rootJsonNode;

    for (int i = 0; i < fieldNames.size(); i++) {
    if (jsonNode.has(fieldNames.get(i))) {
    jsonNode = jsonNode.get(fieldNames.get(i));
    } else {
    return null;
    }
    }

    return jsonNode;
    }


    }
    ---------------------
    版权声明:本文为CSDN博主「现哥女朋友」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/wang_yaqin/article/details/97898648

  • 相关阅读:
    JMeter性能测试
    CentOS7下安装elsticsearch、kibana、filebeat、logstash服务
    Docker容器 之 Harbor 私有镜像仓库的搭建
    Vagrant and VirtualBox 构建 CentOS7
    java 位运算 之 左移和右移理解
    List如果一边遍历,一边删除,且不会报错呢?
    享元模式
    装饰器模式
    日常记录
    销售与营销有什么区别?
  • 原文地址:https://www.cnblogs.com/ly570/p/11329158.html
Copyright © 2011-2022 走看看