<>GSON Usage of ( Processing objects and JSON Mutual transformation of ):

Gson yes Google I wrote one myself Java Object and JSON Mutual transformation Toolkit , First we need to download jar package , Available here idea Direct download , Download steps are as follows ; Click project structure

click + number :

click maven:

Then search jar Package keyword gson:

Download to the specified location of the project

Write a JSON file
[ { "id": 9223232323, "text": "How do I read a JSON stream in Java", "geo":
null, "user": { "name": "json_new", "followers_count": 41 } }, { "id":
8787879686, "text": "@Json just use JsonReader", "geo": [ 56.23212323,
-1034.343 ], "user": { "name": "java nb", "followers_count": 2 } } ]
Corresponding entity class :
package com.lili.json; import java.util.Arrays; import java.util.List; /** *
@author: QiJingJing * @create: 2021/7/18 */ public class Message { private long
id; private String text; private List<Double> geo; private User user; public
long getId() { return id; } public void setId(long id) { this.id = id; } public
String getText() { return text; } public void setText(String text) { this.text =
text; } public List<Double> getGeo() { return geo; } public void setGeo(List<
Double> geo) { this.geo = geo; } public User getUser() { return user; } public
void setUser(User user) { this.user = user; } @Override public String toString()
{ return "Message{" + "id=" + id + ", text='" + text + '\'' + ", geo=" + geo +
", user=" + user + '}'; } } package com.lili.json; /** * @author: QiJingJing *
@create: 2021/7/18 */ public class User { private String name; private int
followers_count; public String getName() { return name; } public void setName(
String name) { this.name = name; } public int getFollowers_count() { return
followers_count; } public void setFollowers_count(int followers_count) { this.
followers_count= followers_count; } @Override public String toString() { return
"User{" + "name='" + name + '\'' + ", followers_count=" + followers_count + '}';
} }
Complex code parsing JSON:
public class JsonTest { /** * use JsonReader Parsing a complex json */ @Test public void
parseJsonMessages() throws IOException { InputStream is = Thread.currentThread()
.getContextClassLoader().getResourceAsStream("com/lili/json/message.json");
InputStreamReader reader = new InputStreamReader(is); JsonReader jsonReader =
new JsonReader(reader); List<Message> list = new ArrayList<>(); jsonReader.
beginArray(); while (jsonReader.hasNext()){ list.add(parseJsonMessage(jsonReader
)); } jsonReader.endArray(); System.out.println(Arrays.toString(list.toArray()))
; } /** * analysis message Class method */ private Message parseJsonMessage(JsonReader reader)
throws IOException { Message message = null; reader.beginObject(); message = new
Message(); while (reader.hasNext()) { String name = reader.nextName(); if("id".
equals(name)){ message.setId(reader.nextLong()); }else if("text".equals(name)){
message.setText(reader.nextString()); }else if("geo".equals(name) && reader.peek
() != JsonToken.NULL){ message.setGeo(readGeo(reader)); }else if("user".equals(
name)){ message.setUser(parseJsonUser(reader)); }else { // Better add reader.skipValue
(); } } reader.endObject(); return message; } /** * analysis geo */ private List<Double
> readGeo(JsonReader reader) throws IOException { List<Double> list = new
ArrayList<>(); reader.beginArray(); while (reader.hasNext()){ list.add(reader.
nextDouble()); } reader.endArray(); return list; } /** * analysis user Class method */ public
User parseJsonUser(JsonReader reader) throws IOException { User user = null;
reader.beginObject(); user = new User(); while (reader.hasNext()) { String name
= reader.nextName(); if("name".equals(name)){ user.setName(reader.nextString());
}else if("followers_count".equals(name)){ user.setFollowers_count(reader.nextInt
()); }else{ // Better add reader.skipValue(); } } reader.endObject(); return user; } }
Operation results :
[Message{id=9223232323, text='How do I read a JSON stream in Java', geo=null,
user=User{name='json_new', followers_count=41}}, Message{id=8787879686,
text='@Json just use JsonReader', geo=[56.23212323, -1034.343],
user=User{name='java nb', followers_count=2}}]
utilize Gson Tool classes will be a set of JSON Object to object collection , Or convert a collection of objects to JSON array :

Write a new one JSON file :
[ { "firstName": "qi", "lastName": "jing", "email": "2811157481@qq.com" }, {
"firstName": "yang", "lastName": "li", "email": "2811157481@qq.com" } ]
Corresponding entity class :
package com.lili.json; /** * @author: QiJingJing * @create: 2021/7/18 */ public
class Name { private String firstName; private String lastName; private String
email; public Name() { } public Name(String firstName, String lastName, String
email) { this.firstName = firstName; this.lastName = lastName; this.email =
email; } public String getFirstName() { return firstName; } public void
setFirstName(String firstName) { this.firstName = firstName; } public String
getLastName() { return lastName; } public void setLastName(String lastName) {
this.lastName = lastName; } public String getEmail() { return email; } public
void setEmail(String email) { this.email = email; } @Override public String
toString() { return "Name{" + "firstName='" + firstName + '\'' + ", lastName='"
+ lastName + '\'' + ", email='" + email + '\'' + '}'; } }
Write test code :
/** * Put a group JSON Object to object collection , Or convert a collection of objects to JSON array */ @Test public void gson2(){ Gson gson
= new Gson(); InputStream resourceAsStream = Thread.currentThread().
getContextClassLoader().getResourceAsStream("com/lili/json/name.json"); assert
resourceAsStream!= null; InputStreamReader reader = new InputStreamReader(
resourceAsStream); TypeToken<List<Name>> typeToken = new TypeToken<List<Name>>()
{}; List<Name> list = gson.fromJson(reader, typeToken.getType()); System.out.
println(list); // Turn back String json = gson.toJson(list, typeToken.getType());
System.out.println(json); }
Object collection and json The data are as follows :
[Name{firstName='qi', lastName='jing', email='2811157481@qq.com'},
Name{firstName='yang', lastName='li', email='2811157481@qq.com'}]
[{"firstName":"qi","lastName":"jing","email":"2811157481@qq.com"},{"firstName":"yang","lastName":"li","email":"2811157481@qq.com"}]

Technology
©2019-2020 Toolsou All rights reserved,
Redis Underlying data structure A person who dissuades others from learning computer , Are not good people win10 System computer C Where's the disc ,c disc users where? (win10c Disk not found users) Freshman c Language student management system ( lower ) hospital WIFI Which family is strong ? utilize Python Script unlimited spoof girlfriend computer C language program design —— Student management system Byte runout - Test development practice - One side cool meridian python Run code at specified time 5 Best style Linux Server system recommendation Anti anxiety life after naked resignation , I believe everything will have the best arrangement