Java 常用 Json 解析框架

Java 常用 Json 解析框架

常見json解析框架:

1. fastjson

maven 引用:

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

用法:

Student student = StudentUtils.genStudent();
String jsonString = JSON.toJSONString(student);// 序列化
String jsonString1 = StudentUtils.STUDENT_STRING;
Student student1 = JSON.parseObject(jsonString1, Student.class);// 反序列化

2. Gson

maven 引用:

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

用法:

Student student = StudentUtils.genStudent();
Gson gson = new Gson();
String jsonString = gson.toJson(student); // 序列化
System.out.println(jsonString);

String jsonString1 = StudentUtils.STUDENT_STRING;
Student student1 = gson.fromJson(jsonString1, Student.class); // 反序列化
System.out.println(student1);

3. Jackson

maven 引用:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.8</version>
</dependency>

用法:

Student student = StudentUtils.genStudent();
ObjectMapper objectMapper = new ObjectMapper();
String jsonString = objectMapper.writeValueAsString(student); // 序列化
System.out.println(jsonString);

String jsonString1 = StudentUtils.STUDENT_STRING;
Student student1 = objectMapper.readValue(jsonString1, Student.class); // 反序列化
System.out.println(student1);

Demo 地址

更多文章

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章