From 35465163e8f2a6f7568f55dd2ef7bd3d3af3b911 Mon Sep 17 00:00:00 2001 From: qipengWei Date: Sat, 21 Nov 2020 20:57:27 +0800 Subject: [PATCH 1/2] =?UTF-8?q?json=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 | 5 ++++ .../com/github/hcsp/encapsulation/Main.java | 10 +++++-- .../github/hcsp/encapsulation/Student.java | 30 +++++++++++++++++++ .../github/hcsp/encapsulation/MainTest.java | 2 +- 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index c20b489..3689e99 100644 --- a/pom.xml +++ b/pom.xml @@ -70,6 +70,11 @@ 5.6.0 test + + com.alibaba + fastjson + 1.2.58 + diff --git a/src/main/java/com/github/hcsp/encapsulation/Main.java b/src/main/java/com/github/hcsp/encapsulation/Main.java index 51ce4a1..ba26d83 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,11 @@ public static void main(String[] args) { student = deserialize(json); } // 序列化:将Student类转换成JSON字符串 - public static String serialize(Student student) {} + public static String serialize(Student student) { + return JSON.toJSONString(student); + } // 反序列化:将JSON字符串转换成Student对象 - public static Student deserialize(String json) {} + public static Student deserialize(String json) { + return JSON.parseObject(json,Student.class); + } } diff --git a/src/main/java/com/github/hcsp/encapsulation/Student.java b/src/main/java/com/github/hcsp/encapsulation/Student.java index 85d2f55..398fd06 100644 --- a/src/main/java/com/github/hcsp/encapsulation/Student.java +++ b/src/main/java/com/github/hcsp/encapsulation/Student.java @@ -10,4 +10,34 @@ public class Student { /** 分数 */ private int score; + + private boolean fail; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public boolean isFail() { + return this.score > 60 + ? true + : false; + } + + public void setRetakingExam(boolean retakingExam) { + this.retakingExam = retakingExam; + } + + public int getScore() { + return score; + } + + public void setScore(int score) { + this.score = score; + } + + } diff --git a/src/test/java/com/github/hcsp/encapsulation/MainTest.java b/src/test/java/com/github/hcsp/encapsulation/MainTest.java index 3c5c949..85c1fef 100644 --- a/src/test/java/com/github/hcsp/encapsulation/MainTest.java +++ b/src/test/java/com/github/hcsp/encapsulation/MainTest.java @@ -28,7 +28,7 @@ public void canSerializeAndDeserialize() { s = com.alibaba.fastjson.JSON.parseObject(json, Student.class); Assertions.assertEquals("张三", s.getName()); - Assertions.assertEquals(true, s.isRetakingExam()); + Assertions.assertEquals(true, s.isFail()); Assertions.assertEquals(59, s.getScore()); Assertions.assertEquals(true, s.isFail()); } From 70c89f305dda6590a14d1b023d3031efaa745176 Mon Sep 17 00:00:00 2001 From: qipengWei Date: Sat, 21 Nov 2020 21:06:26 +0800 Subject: [PATCH 2/2] =?UTF-8?q?json=E5=BA=8F=E5=88=97=E5=8C=96=20=E9=A2=98?= =?UTF-8?q?=E7=9B=AE=E6=B2=A1=E7=9C=8B=E6=B8=85?= 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, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/github/hcsp/encapsulation/Student.java b/src/main/java/com/github/hcsp/encapsulation/Student.java index 398fd06..fea2a34 100644 --- a/src/main/java/com/github/hcsp/encapsulation/Student.java +++ b/src/main/java/com/github/hcsp/encapsulation/Student.java @@ -11,7 +11,6 @@ public class Student { /** 分数 */ private int score; - private boolean fail; public String getName() { return name; @@ -22,9 +21,7 @@ public void setName(String name) { } public boolean isFail() { - return this.score > 60 - ? true - : false; + return this.score < 60; } public void setRetakingExam(boolean retakingExam) { @@ -39,5 +36,7 @@ public void setScore(int score) { this.score = score; } - + public boolean isRetakingExam() { + return retakingExam; + } }