[置顶] JSON工具类

JSON工具类有许多种,这里列出三个比较流行的json工具类:Jackson,Gson,FastJson.

FastJson项目地址:https://github.com/alibaba/fastjson

Gson项目地址:https://github.com/google/gson

Jackson项目地址:https://github.com/FasterXML/jackson


相关jar配置,具体版本可以参考:http://search.maven.org/, 建议搭建自己的私服仓库,具体方法可以Google

<!-- jackson begin --> <dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>${codehaus.jackson.version}</version>
</dependency>
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>${codehaus.jackson.version}</version>
</dependency>
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-jaxrs</artifactId>
    <version>${codehaus.jackson.version}</version>
</dependency>
<!-- jackson end -->

<!--  gson begin -->

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.6.2</version>
</dependency>

<!--  gson end -->

<!-- FastJson begin-->

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>${fastjson.version}</version>
</dependency>

<!-- FastJson end -->

1.开源的Jackson

  Jackson社区相对比较活跃,更新速度也比较快。Jackson对于复杂类型的json转换bean会出现问题,一些集合Map,List的转换出现问题。Jackson对于复杂类型的bean转换Json,转换的json格式不是标准的Json格式。

public class JacksonTest{
    //增加JSON串冗余字段解析忽略配置  private static final ObjectMapper objectMapper = new ObjectMapper()
            .configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
   private static final JsonFactory jsonFactory = new JsonFactory();
 
    /**  * deserializea a json string to an object  *  * @param <T>  * @param jsonAsString  * @param pojoClass  * @return  * @throws Exception  */  public static <T> T fromJson(String jsonAsString, Class<T> pojoClass)
         throws Exception {
      return (T)objectMapper.readValue(jsonAsString, pojoClass);
   }

    /**  * deserializea a json string to a Collection  * @param jsonAsString  * @param typeReference  * @param <T>  * @return  */  @SuppressWarnings({ "unchecked", "rawtypes" })
    public static <T> T fromJson(String jsonAsString, TypeReference typeReference) throws IOException {
        return (T) objectMapper.readValue(jsonAsString,typeReference);
    }

   /**  * serializea an object to a json string  * @param pojo  * @param prettyPrint if true,enabling pretty-printing using the default pretty printer  * @return  * @throws Exception  */  public static String toJson(Object pojo, boolean prettyPrint) throws IOException {
      StringWriter sw = new StringWriter();
      JsonGenerator jg = jsonFactory.createJsonGenerator(sw);
      if (prettyPrint) {
         jg.useDefaultPrettyPrinter();
      }
      objectMapper.writeValue(jg, pojo);
      return sw.toString();
   }
   
   
   public static String toJson(Object pojo) throws IOException{
     return toJson(pojo, false);
   }

}
 
 

2. Google的Gson

  Gson是目前功能最全的Json解析神器,Gson当初是为因应Google公司内部需求而由Google自行研发而来,但自从在2008年五月公开发布第一版后已被许多公司或用户应用。Gson的应用主要为toJson与fromJson两个转换函数,无依赖,不需要例外额外的jar,能够直接跑在JDK上。而在使用这种对象转换之前需先创建好对象的类型以及其成员才能成功的将JSON字符串成功转换成相对应的对象。类里面只要有get和set方法,Gson完全可以将复杂类型的json到bean或bean到json的转换,是JSON解析的神器。Gson在功能上面无可挑剔,但是性能上面比FastJson有所差距。

public class GsonTest {
    private static Gson gson=new Gson();

    public static <T> T fromJson(String jsonAsString, Class<T> pojoClass)throws IOException {
        return (T)gson.fromJson(jsonAsString,pojoClass);
    }

    public static String toJson(Object pojo)throws IOException {
        return gson.toJson(pojo);
    }
}
 
 

3. 阿里巴巴的FastJson

  Fastjson是一个Java语言编写的高性能的JSON处理器,由阿里巴巴公司开发。无依赖,不需要例外额外的jar,能够直接跑在JDK上。 
FastJson在复杂类型的Bean转换Json上会出现一些问题,可能会出现引用的类型,导致Json转换出错,需要制定引用。FastJson采用独创的算法,将parse的速度提升到极致,超过所有json库。

public class FastJsonTest {
    public static <T> T fromJson(String jsonAsString, Class<T> pojoClass)throws IOException {
        return (T)JSON.parseObject(jsonAsString,pojoClass);
    }

    public static String toJson(Object pojo)throws IOException {
        return JSON.toJSONString(pojo);
    }
}

 
 


你可能感兴趣的:(java,json)