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) {(http://www.amjmh.com/v/BIBRGZ_558768/)
    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;
    }


    }
    --------------------- 

  • 相关阅读:
    NOI2013
    【FINAL】NOI
    【jsoi】第一季 [略]精简题解
    浏览器缓存机制--小总结
    UC和QQ两个主流浏览器 * 点击触发微信分享到朋友圈或发送给朋友的功能(转载)
    webpack加载postcss,以及autoprefixer的loader
    HTTP协议中的短轮询、长轮询、长连接和短连接,看到一篇文章有感
    webpack2.0 css文件引入错误解决及图片输出在根目录配置问题
    webpack+vue 我的视角(持续更新)
    手机端图像编辑上传-cropper
  • 原文地址:https://www.cnblogs.com/ly570/p/11329171.html
Copyright © 2011-2022 走看看