zoukankan      html  css  js  c++  java
  • JSON 的几种转换

      6 package com.qbskj.project.util;
      7 
      8 import java.io.IOException;
      9 import java.io.Writer;
     10 
     11 import org.springframework.util.Assert;
     12 
     13 import com.fasterxml.jackson.core.JsonGenerationException;
     14 import com.fasterxml.jackson.core.type.TypeReference;
     15 import com.fasterxml.jackson.databind.JavaType;
     16 import com.fasterxml.jackson.databind.JsonMappingException;
     17 import com.fasterxml.jackson.databind.ObjectMapper;
     18 
     19 /**
     20  * Utils - JSON
     21  */
     22 public final class JsonUtils {
     23 
     24     /** ObjectMapper */
     25     private static ObjectMapper mapper = new ObjectMapper();
     26 
     27     /**
     28      * 不可实例化
     29      */
     30     private JsonUtils() {
     31     }
     32 
     33     /**
     34      * 将对象转换为JSON字符串
     35      * 
     36      * @param value
     37      *            对象
     38      * @return JSOn字符串
     39      */
     40     public static String toJson(Object value) {
     41         try {
     42             return mapper.writeValueAsString(value);
     43         } catch (Exception e) {
     44             e.printStackTrace();
     45         }
     46         return null;
     47     }
     48 
     49     /**
     50      * 将JSON字符串转换为对象
     51      * 
     52      * @param json
     53      *            JSON字符串
     54      * @param valueType
     55      *            对象类型
     56      * @return 对象
     57      */
     58     public static <T> T toObject(String json, Class<T> valueType) {
     59         Assert.hasText(json, "");
     60         Assert.notNull(valueType, "");
     61         try {
     62             return mapper.readValue(json, valueType);
     63         } catch (Exception e) {
     64             e.printStackTrace();
     65         }
     66         return null;
     67     }
     68 
     69     /**
     70      * 将JSON字符串转换为对象
     71      * 
     72      * @param json
     73      *            JSON字符串
     74      * @param typeReference
     75      *            对象类型
     76      * @return 对象
     77      */
     78     public static <T> T toObject(String json, TypeReference<?> typeReference) {
     79         Assert.hasText(json, "");
     80         Assert.notNull(typeReference, "");
     81         try {
     82             return mapper.readValue(json, typeReference);
     83         } catch (Exception e) {
     84             e.printStackTrace();
     85         }
     86         return null;
     87     }
     88 
     89     /**
     90      * 将JSON字符串转换为对象
     91      * 
     92      * @param json
     93      *            JSON字符串
     94      * @param javaType
     95      *            对象类型
     96      * @return 对象
     97      */
     98     public static <T> T toObject(String json, JavaType javaType) {
     99         Assert.hasText(json, "");
    100         Assert.notNull(javaType, "");
    101         try {
    102             return mapper.readValue(json, javaType);
    103         } catch (Exception e) {
    104             e.printStackTrace();
    105         }
    106         return null;
    107     }
    108 
    109     /**
    110      * 将对象转换为JSON流
    111      * 
    112      * @param writer
    113      *            writer
    114      * @param value
    115      *            对象
    116      */
    117     public static void writeValue(Writer writer, Object value) {
    118         try {
    119             mapper.writeValue(writer, value);
    120         } catch (JsonGenerationException e) {
    121             e.printStackTrace();
    122         } catch (JsonMappingException e) {
    123             e.printStackTrace();
    124         } catch (IOException e) {
    125             e.printStackTrace();
    126         }
    127     }
    128 
    129 }
    成功不是终点,失败也并非末日,重要的是前行的勇气!
  • 相关阅读:
    跨域解决方法
    css之line-height
    untiy项目中使用MD5加密
    unity给子物体添加Shader
    unity中UI坐标转3d世界坐标
    unity项目字符串转为Vector3和Quaternion
    unity中使用Highlighting System v4.0插件给物体添加高亮
    加载AssetBundle方法
    Lua面向对象----类、继承、多继承、单例的实现
    Lua学习笔记(一)-----C#和lua的交互
  • 原文地址:https://www.cnblogs.com/DSH-/p/10791098.html
Copyright © 2011-2022 走看看