Java JSON库常用资源

Java常用JSON库

  • Gson
  • Json-lib
  • Jackson
  • FastJson

Json-lib

Maven依赖:

<dependency>
    <groupId>net.sf.json-lib</groupId>
    <artifactId>json-lib</artifactId>
    <version>2.4</version>
    <classifier>jdk15</classifier>
</dependency>

模版工具类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import net.sf.json.JSONObject;

public class JsonObjectUtil {

public static String bean2Json(Object obj){
JSONObject jsonObject=JSONObject.fromObject(obj);
return jsonObject.toString();
}

@SuppressWarnings("unchecked")
public static <T> T json2Bean(String jsonStr,Class<T> objClass){
return (T)JSONObject.toBean(JSONObject.fromObject(jsonStr), objClass);
}

}

Gson

Maven依赖:

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

模版工具类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

public class GsonUtil {
private static Gson gson = new GsonBuilder().create();

public static String bean2Json(Object obj){
return gson.toJson(obj);
}

public static <T> T json2Bean(String jsonStr,Class<T> objClass){
return gson.fromJson(jsonStr, objClass);
}

public static String jsonFormatter(String uglyJsonStr){
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(uglyJsonStr);
String prettyJsonString = gson.toJson(je);
return prettyJsonString;
}
}

Jackson

Maven依赖:

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
</dependency>

模版工具类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.io.IOException;
import java.io.StringWriter;

import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class JacksonUtil {
private static ObjectMapper mapper = new ObjectMapper();

public static String bean2Json(Object obj) throws IOException {
StringWriter sw = new StringWriter();
JsonGenerator gen = new JsonFactory().createJsonGenerator(sw);
mapper.writeValue(gen, obj);
gen.close();
return sw.toString();
}

public static <T> T json2Bean(String jsonStr, Class<T> objClass)
throws JsonParseException, JsonMappingException, IOException {
return mapper.readValue(jsonStr, objClass);
}
}

FastJson

Maven依赖:

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.7</version>
</dependency>

模版工具类:

1
2
3
4
5
6
7
8
9
10
11
import com.alibaba.fastjson.JSON;

public class FastJsonUtil {
public static String bean2Json(Object obj){
return JSON.toJSONString(obj);
}

public static <T> T json2Bean(String jsonStr,Class<T> objClass){
return JSON.parseObject(jsonStr, objClass);
}
}