From c88762c95a60fee80f896356f5502154d6dde572 Mon Sep 17 00:00:00 2001 From: lee Date: Wed, 1 Dec 2021 15:59:19 +0000 Subject: [PATCH 1/2] =?UTF-8?q?Json=E7=9A=84=E5=BA=8F=E5=88=97=E5=8C=96?= =?UTF-8?q?=E4=B8=8E=E5=8F=8D=E5=BA=8F=E5=88=97=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 ++++ .../com/github/hcsp/encapsulation/Main.java | 15 ++++++-- .../github/hcsp/encapsulation/Student.java | 34 +++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index c20b489..adff797 100644 --- a/pom.xml +++ b/pom.xml @@ -70,8 +70,14 @@ 5.6.0 test + + com.alibaba + fastjson + 1.2.76 + + diff --git a/src/main/java/com/github/hcsp/encapsulation/Main.java b/src/main/java/com/github/hcsp/encapsulation/Main.java index 51ce4a1..d8483fc 100644 --- a/src/main/java/com/github/hcsp/encapsulation/Main.java +++ b/src/main/java/com/github/hcsp/encapsulation/Main.java @@ -1,5 +1,7 @@ package com.github.hcsp.encapsulation; +import com.alibaba.fastjson.JSON; + public class Main { /* 假设你正在为学校开发一个学生分数记录系统 @@ -26,7 +28,16 @@ public static void main(String[] args) { student = deserialize(json); } // 序列化:将Student类转换成JSON字符串 - public static String serialize(Student student) {} + public static String serialize(Student student) { + String jsonToString = JSON.toJSONString(student); + return jsonToString; + + + } + // 反序列化:将JSON字符串转换成Student对象 - public static Student deserialize(String json) {} + public static Student deserialize(String json) { + Student stringToJson = JSON.parseObject(json,Student.class); + return stringToJson; + } } diff --git a/src/main/java/com/github/hcsp/encapsulation/Student.java b/src/main/java/com/github/hcsp/encapsulation/Student.java index 85d2f55..97881c3 100644 --- a/src/main/java/com/github/hcsp/encapsulation/Student.java +++ b/src/main/java/com/github/hcsp/encapsulation/Student.java @@ -10,4 +10,38 @@ public class Student { /** 分数 */ private int score; + + private boolean Fail; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public boolean isRetakingExam() { + return retakingExam; + } + + public void setRetakingExam(boolean retakingExam) { + this.retakingExam = retakingExam; + } + + public int getScore() { + return score; + } + + public void setScore(int score) { + this.score = score; + } + + public boolean isFail() { + return Fail; + } + + public void setFail(boolean fail) { + Fail = fail; + } } From a1521a5df22d760021a14c0d17757e0ac30e5b12 Mon Sep 17 00:00:00 2001 From: lee Date: Wed, 1 Dec 2021 18:10:33 +0000 Subject: [PATCH 2/2] =?UTF-8?q?Json=E7=9A=84=E5=BA=8F=E5=88=97=E5=8C=96?= =?UTF-8?q?=E4=B8=8E=E5=8F=8D=E5=BA=8F=E5=88=97=E5=8C=96=5Fcheck=20the=20s?= =?UTF-8?q?core=20fail=20or=20not?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/github/hcsp/encapsulation/Student.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/github/hcsp/encapsulation/Student.java b/src/main/java/com/github/hcsp/encapsulation/Student.java index 97881c3..f5e150f 100644 --- a/src/main/java/com/github/hcsp/encapsulation/Student.java +++ b/src/main/java/com/github/hcsp/encapsulation/Student.java @@ -38,7 +38,14 @@ public void setScore(int score) { } public boolean isFail() { - return Fail; + if (score < 60) { + return true; + } else { + return false; + } + + + } public void setFail(boolean fail) {