本文为学习类文档,通过学习B站up主longway777的视频,再加上自己的总结与理解的学习类文章,如有侵权,请联系博主进行删除
关于JSON:
全称为:基于JavaScript语言的轻量级的数据交换格式(JavaScript Object Notation)
JavaScript是一种网络上很流行的脚本语言,JSON是一种格式,类似于XML,均为使用键值对(key-value)记录内容。
JSON可以理解为XML的精简内容
为了节省流量,使用轻量化的JSON传输网络上的数据便成为了主流。
JSON以一对{}来包含,里面以“ “” ”引号
创建一个JSON草稿(scratch类型的JSON文件):
{
"name": "Jack",
"age": 20
}
public class student {
String name;
int age;
public student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
GSON是谷歌自家的工具:
https://github.com/google/gson
是一个Java的序列化和反序列化的一个开源库,用来转换Java对象为JSON格式或反过来转换(双向操作)
使用GSON需要添加依赖:初次下载请保持网络通畅
dependencies {
implementation 'com.google.code.gson:gson:2.8.6'
}
在主界面MianActivity中添加逻辑代码:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.google.gson.Gson;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Student student1 = new Student("Jack",20);
//转变JSON字符串,使用JSON工具
Gson gson = new Gson();
String jsonStudent1 = gson.toJson(student1); //呼叫json方法
}
}
由于此处没有设置输入输出,可以选择断点测试
系统会自动跳转到测试界面
可观察到之前创建的均已实现:
将最后一行的GSON粘贴到之前的JSON草稿中重整代码格式进行对比就可以看到完全一致:
关于反序列化:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.google.gson.Gson;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//反向操作,反序列化
Gson gson1 = new Gson();
String jsonStudent2 = "{\"age\":20,\"name\":\"Jack\"}"; //粘贴上面调试的json格式序列
Student student2 = gson1.fromJson(jsonStudent2,Student.class); //传参(上面的字符串和传递的对象类型)
}
}
丰富student类,新创建一个score类
Student.java
public class Student {
private String name;
private int age;
private Score score;
public Student(String name, int age, Score score) {
this.name = name;
this.age = age;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Score getScore() {
return score;
}
public void setScore(Score score) {
this.score = score;
}
}
Score.java
public class Score {
private int math;
private int English;
private int Chinese;
public Score(int math, int english, int chinese) {
this.math = math;
English = english;
Chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEnglish() {
return English;
}
public void setEnglish(int english) {
English = english;
}
public int getChinese() {
return Chinese;
}
public void setChinese(int chinese) {
Chinese = chinese;
}
}
主界面代码:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.google.gson.Gson;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//复杂情况下的序列化与反序列化
Gson gson = new Gson();
Student student3 = new Student("Tom",19,new Score(90,90,90));
String jsonStudent3 = gson.toJson(student3);
}
}
通过debug调试发现创建序列化成功,将序列化code复制到json草稿中并重整代码格式(Code->Reformat Code)查看:
反序列化同理:
package com.example.json_demo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.google.gson.Gson;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Gson gson1 = new Gson();
String jsonStudent4 = "{\"age\":19,\"name\":\"Tom\",\"score\":{\"Chinese\":90,\"English\":90,\"math\":90}}";
Student student4;
student4 = gson1.fromJson(jsonStudent4,Student.class);
}
}
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.google.gson.Gson;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Gson gson = new Gson();
Student student5 = new Student("Mike",22,new Score(80,80,80));
Student student6 = new Student("Lisa",21,new Score(99,99,99));
Student[] students = {student5,student6}; //创建Array数组
String jsonStudents = gson.toJson(students);
}
}
断点debug调试可得到json型序列化的数据
放入json草稿中进行代码重整即可
json[]方括号表示一个矩阵
反序列化:
Gson gson = new Gson();
String jsonStudents = "[{\"age\":22,\"name\":\"Mike\",\"score\":{\"Chinese\":80,\"English\":80,\"math\":80}},{\"age\":21,\"name\":\"Lisa\",\"score\":{\"Chinese\":99,\"English\":99,\"math\":99}}]";
Students[] students = gson.fromJson(jsonStudents,Student[].class);
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//包装List进行序列化
Gson gson = new Gson();
Student student7 = new Student("Tom",3,new Score(60,60,60));
Student student8 = new Student("Jerry",1,new Score(99,99,99));
List studentList = new ArrayList<>();
studentList.add(student7);
studentList.add(student8);
String jsonStudents = gson.toJson(studentList);
Gson gson1 = new Gson();
String jsonStudents1 = "[{\"age\":3,\"name\":\"Tom\",\"score\":{\"Chinese\":60,\"English\":60,\"math\":60}},{\"age\":1,\"name\":\"Jerry\",\"score\":{\"Chinese\":99,\"English\":99,\"math\":99}}]";
List studentList1 = new ArrayList<>();
//由于无法得到反序列化的类型,多一步操作
Type typeStudents = new TypeToken>(){}.getType(); //创建一个新类型
studentList1 = gson1.fromJson(jsonStudents1,typeStudents);
}
}
补充:在类名处可以自定义序列化的名称
@SerializedName("student_name") //防止与同行操作的序列化冲突,可自行定义自己的序列化名称
添加对象的自定义序列化名称(此处列举一个name的自定义):
主界面再次创建json序列化
Gson gson2 = new Gson();
Student student9 = new Student("Ken",23,new Score(0,0,0));
String jsonStudent9 = gson.toJson(student9);