“This is the 14th day of my participation in the Gwen Challenge.
Gson is a Google open source JSon parsing library. We generally use FastJSON in China, which is based on Ali open source. However, there have been problems with its standardization, so some projects will use this library for parsing:
JSON: Lightweight interactive language;
Json(JavaScript Object Notation) Object Notation
A lightweight data interaction format that is easy to transfer and write, complete in a language-independent text format
It is generally used as a standard for transferring business data between clients and servers
2.JSON exists in two forms;
One is the object, json object: (Get the data inside, content, key-value pairs — json object)
The other is strings, JSON strings; (Use JSON strings when server and client interact)
3. Application in Java:
I’m using Google GSON
<! -- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
The < version > 2.8.6 < / version >
</dependency>
Copy the code
You can also use JSON from Hutool
3.1 Converting Javabeans to JSON:
package com.jsonTest.test;
import com.google.gson.reflect.TypeToken;
import com.google.gson.Gson;
import com.jsonTest.entity.Student;
import com.jsonTest.entity.StudentListType;
import com.jsonTest.entity.StudentMapType;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class GsonTest {
/ * *
* Javabeans in json
* /
@Test
public void test1(a){
Student student=new Student("1"."web");
Gson gson=new Gson();
String toJson = gson.toJson(student);
System.out.println(student);
System.out.println("Normal object becomes JSON"+toJson);
/ * *
* Student(id=1, name=web)
Json {"id":"1","name":"web"}
* /
/ * *
* Convert the current JSON to a Javabean
* /
Student student1 = gson.fromJson(toJson, Student.class);
System.out.println(student1);
}
@Test
public void TestList(a){
List<Student> studentList=new ArrayList<>();
studentList.add(new Student("2"."Path card"));
studentList.add(new Student("3"."Little Luca"));
Gson gson=new Gson();
/ / the list into json
String jsonList = gson.toJson(studentList);
System.out.println("lIST"+studentList);
System.out.println("list-->JSON"+jsonList);
/ * *
* lIST[Student(id=2, name= Student), Student(id=3, name= Student)]
* the list - > JSON [{" id ":" 2 ", "name" : "path card"}, {" id ":" 3 ", "name" : "small luca"}]
* /
// jsonList becomes an object or List
List list = gson.fromJson(jsonList, List.class);
System.out.println(list);// This will result in a cast exception
/ * *
* A type is required to convert a lIST to an object
* /
List list1 = gson.fromJson(jsonList, new StudentListType().getType());
System.out.println(list1);
}
@Test
public void TestMap(a){
Map<String,Student> studentMap=new HashMap();
studentMap.put("1".new Student("4"."lucas"));
studentMap.put("2".new Student("5"."xiaoLucas"));
Gson gson=new Gson();
// Convert map to JSON
String toJsonMap = gson.toJson(studentMap);
System.out.println("jsonMap"+toJsonMap);
/ * *
* jsonMap{"1":{"id":"4","name":"lucas"},"2":{"id":"5","name":"xiaoLucas"}}
* /
/ * *
* Convert the map into individual objects
* /
Map<String,Student> student = gson.fromJson(toJsonMap, new StudentMapType().getType());
System.out.println(student);
Student student1 = student.get(1);
System.out.println(student1);
/ * *
* The new Type class appears at every conversion, so we can use anonymous inner classes
* /
}
@Test
public void TestMap2(a){
Map<String,Student> studentMap=new HashMap();
studentMap.put("1".new Student("4"."lucas"));
studentMap.put("2".new Student("5"."xiaoLucas"));
Gson gson=new Gson();
// Convert map to JSON
String toJsonMap = gson.toJson(studentMap);
System.out.println("jsonMap"+toJsonMap);
/ * *
* jsonMap{"1":{"id":"4","name":"lucas"},"2":{"id":"5","name":"xiaoLucas"}}
* /
/ * *
* Convert the map into individual objects
* /
Map<String,Student> student = gson.fromJson(toJsonMap,new TypeToken<Map<String,Student>>(){}.getType());
System.out.println(student);
Student student1 = student.get("1");
System.out.println(student1);
/ * *
* jsonMap{"1":{"id":"4","name":"lucas"},"2":{"id":"5","name":"xiaoLucas"}}
* {1=Student(id=4, name=lucas), 2=Student(id=5, name=xiaoLucas)}
* Student(id=4, name=lucas)
* /
/ * *
* The new Type class appears at every conversion, so we can use anonymous inner classes
* /
}
}
Copy the code
package com.jsonTest.entity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.io.Serializable;
@EqualsAndHashCode
@Data
public class Student implements Serializable {
private String id ;
private String name;
public Student(String s, String web) {
this.id=s;
this.name=web;
}
}
Copy the code
package com.jsonTest.entity;
import com.google.gson.reflect.TypeToken;
import java.util.List;
/ * *
* When the List is converted, json is converted to a Type that inherits the typeToken class
* /
public class StudentListType extends TypeToken<List<Student>> {
}
Copy the code
package com.jsonTest.entity;
import com.google.gson.reflect.TypeToken;
import java.util.HashMap;
public class StudentMapType extends TypeToken<HashMap<String,Student>> {
}
Copy the code
Parsing Java beans versus JSON: There are other techniques we can use for parsing:
Luca’s message:
For normal JSON parsing, we can use JSONutil in Hutool,
FastJSON,Gson, or even your own manual can also be used, in fact, this is mainly for the format of objects parsed into POJO.
In the actual development process, JSONUTIL is often used for parsing. The tool library of Hutool is commonly used in Java development, such as POI,IO,JSON, Bloom filtering, MD5, token and so on, which can be packaged well.
Hutool’s website
Quick start
I’m Luca, try to be a technology exporter, and cheer up today.