一、常用JSON类库
- FastJson:阿里巴巴公司开发,无依赖(个人推荐使用)
- Gson:目前功能最全的Json解析器,Google研发,无依赖
- Jackson:Spring MVC的默认json解析器
- Json-lib:已淘汰
二、Gson
1、jar包
gson.jar
Maven:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
2、常用功能方法
2.1 Gson
- java类序列化toJson()
- java类反序列化fromJson()
//1、声明Gson对象
Gson gson = new Gson();
Map<String, Object> map = new HashMap<String, Object>();
map.put("1", 1);
map.put("2", 2);
//2、转成json字符串
String str = gson.toJson(map);
System.out.println(str);
//3、基本上可以转成任何类型的实例
JsonObject json = gson.fromJson(str, JsonObject.class);
System.out.println(json);
2.2 JsonObject
//1、声明json对象
JsonObject jsonObject = new JsonObject();
//2、添加数据
jsonObject.addProperty("1", "1");
jsonObject.addProperty("2", "2");
2.3 JsonArray
/声明jsonArray对象
JsonArray jsonArray = new JsonArray();
//添加数据
JsonObject jsonObject1 = new JsonObject();
jsonObject1.addProperty("1", "1");
jsonObject1.addProperty("2", "2");
JsonObject jsonObject2 = new JsonObject();
jsonObject2.addProperty("3", "3");
jsonObject2.addProperty("4", "3");
jsonArray.add(jsonObject1);
jsonArray.add(jsonObject2);
//直接拿到JsonObject
JsonObject jsonObject = jsonArray.get(0).getAsJsonObject();
三、FastJson
fastjson.jar
Maven:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
2、常用功能方法
2.1 JSON序列化和反序列化
//定义user类
public class User {
private Long id;
private String name;
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
//序列化
public static void serialize() {
User user = new User();
user.setId(11L);
user.setName("西安");
user.setCreateTime(new Date());
String jsonString = JSON.toJSONString(user);
System.out.println(jsonString);
}
//反序列化
public static void deserialize() {
String jsonString = "{\"createTime\":\"2018-08-17 14:38:38\",\"id\":11,\"name\":\"西安\"}";
User user = JSON.parseObject(jsonString, User.class);
System.out.println(user.getName());
System.out.println(user.getCreateTime());
}
2.2 JSONObject
put(),get(),size(),isEmpty(),getJSONObject(),getJSONArray()
//新建JSONObject对象
JSONObject object1 = new JSONObject();
//1.在JSONObject对象中放入键值对
object1.put("name", "张三");
//2.根据key获取value
String name = (String) object1.get("name");
//3.获取JSONObject中的键值对个数
int size = object1.size();
//4.判断是否为空
boolean result = object1.isEmpty();
//5.是否包含对应的key值,包含返回true,不包含返回false
boolean isContainsKeyResult = object1.containsKey("name");
//6.JSONObjct对象中的value是一个JSONObject对象,即根据key获取对应的JSONObject对象;
JSONObject object2 = new JSONObject();
//将jsonobject对象作为value进行设置
object2.put("student1", object1);
JSONObject student =object2.getJSONObject("student1");
System.out.println(student);
//7.如果JSONObject对象中的value是一个JSONObject数组,既根据key获取对应的JSONObject数组;
JSONObject objectArray = new JSONObject();
//创建JSONArray数组
JSONArray jsonArray = new JSONArray();
//在JSONArray数组设值:jsonArray.add(int index, Object value);
jsonArray.add(0, "this is a jsonArray value");
jsonArray.add(1, "another jsonArray value");
objectArray.put("testArray", jsonArray);
//获取JSONObject对象中的JSONArray数组
JSONArray jsonArray2 = objectArray.getJSONArray("testArray");
System.out.println(jsonArray2);
2.3 JSONArray
相当于List<Object>
四、Jackson
1、jar包
jackson-core.jar 核心包
jackson-annotations.jar 注解包,提供标准注解功能
jackson-databind.jar 数据绑定包
Maven:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.1</version>
</dependency>
2、常用功能方法
2.1 ObjectMapper
readValue(),writeValueAsString()
//序列化
User user = new User();
user.setName("小民");
user.setEmail("xiaomin@sina.com");
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(user);
System.out.println(json);
//反序列化
String json = "{\"name\":\"小民\",\"age\":20,\"email\":\"xiaomin@sina.com\"}";
ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(json, User.class);
序列化可以使用注解