1、Gson
1)添加依赖
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
2)转化方法
(1)对象转Json
Gson gson = new Gson();
String json = gson.toJson(Object object);
(2)Json转对象
gson.fromJson(String json, Class<T> classOfT)
(3)集合转Json
Gson gson = new Gson();
String json = gson.toJson(Object object);
(4)Json转集合
TypeToken<T> typeOfT = new TypeToken<T>(){};
T fromJson = (T)gson.fromJson(json, typeOfT.getType());
2、Json-Lib
1)添加依赖
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
2)转化方法
(1)对象转Json
JSONObject fromObject = JSONObject.fromObject(Object object);
String string = fromObject.toString();
(2)Json转对象
JSONObject fromObject2 = JSONObject.fromObject(string);
Object bean =JSONObject.toBean(JSONObject jsonObject, Class beanClass)
(3)集合转Json
JSONArray fromObject = JSONArray.fromObject(Object object);
String string = fromObject.toString();
(4)Json转集合
JSONArray fromObject2 = JSONArray.fromObject(string);
Collection collection = JSONArray.toCollection
(JSONArray jsonArray, Class objectClass)
3、Fastjson
Fastjson是阿里巴巴公司开发的,Java语言编写的,JSON的处理器。
1)添加依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.31</version>
</dependency>
2)转化方法
(1)对象转Json
JSON.toJSONString(Object object);
(2)Json转对象
JSON.parseObject(String text,Class<T> Class);
(3)集合转Json
JSON.toJSONString(Object object)
(4)Json转集合
JSON.parseArray(String text,Class<T> Class);
4、Jackson
1)添加依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.8</version>
</dependency>
2)转换方法
public static String object_to_json(Object object) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(object);
}
public static <T> T json_to_object(String json, Class<T> mainClass, Class<?>... parametricClasses) throws JsonParseException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
JavaType parametricType = mapper.getTypeFactory().constructParametricType(mainClass, parametricClasses);
T readValue = mapper.readValue(json, parametricType);
return readValue;
}