
Java 9 引入了一个交互式REPL 命令行工具,名为JShell。它允许我们执行Java代码片段并立即获得结果。我们可以导入可以从JShell会话中访问的外部类,通过类路径。 Gson库是一个Java序列化/反序列化库,用于将Java对象转换为JSON,反之亦然。
在下面的代码片段中,我们可以在JShell中设置类路径
<strong>jshell> /env --class-path C:\Users\User\gson.jar | Setting new options and restoring state.</strong>
立即学习“Java免费学习笔记(深入)”;
Android文档-开发者指南-第一部分:入门-中英文对照版 Android提供了丰富的应用程序框架,它允许您在Java语言环境中构建移动设备的创新应用程序和游戏。在左侧导航中列出的文档提供了有关如何使用Android的各种API来构建应用程序的详细信息。第一部分:Introduction(入门) 0、Introduction to Android(引进到Android) 1、Application Fundamentals(应用程序基础) 2、Device Compatibility(设备兼容性) 3、
11
立即学习“Java免费学习笔记(深入)”;
一旦我们在JShell中导入了gson 库 ,就能在列表中看到该库。
<strong>jshell> import com.google.gson.*
jshell> /import
| import java.io.*
| import java.math.*
| import java.net.*
| import java.nio.file.*
| import java.util.*
| import java.util.concurrent.*
| import java.util.function.*
| import java.util.prefs.*
| import java.util.regex.*
| import java.util.stream.*
| import com.google.gson.*
jshell> Gson g = new GsonBuilder().setPrettyPrinting().create()
g ==> {serializeNulls:false,factories:[Factory[typeHier ... 78b9],instanceCreators:{}}</strong>立即学习“Java免费学习笔记(深入)”;
立即学习“Java免费学习笔记(深入)”;
在下面的代码片段中,我们创建了一个Employee 类。
<strong>jshell> class Employee {
...> private String firstName;
...> private String lastName;
...> private String designation;
...> private String location;
...> public Employee(String firstName, String lastName, String desigation, String location) {
...> this.firstName = firstName;
...> this.lastName = lastName;
...> this.designation = designation;
...> this.location = location;
...> }
...> public String getFirstName() {
...> return firstName;
...> }
...> public String getLastName() {
...> return lastName;
...> }
...> public String getJobDesignation() {
...> return designation;
...> }
...> public String getLocation() {
...> return location;
...> }
...> public String toString() {
...> return "Name = " + firstName + ", " + lastName + " | " +
...> "Job designation = " + designation + " | " +
...> "location = " + location + ".";
...> }
...> }
| created class Employee
jshell> Employee e = new Employee("Jai", "Adithya", "Content Developer", "Hyderabad");
e ==> Name = Jai, Adithya | Job designation = Content D ... er | location = Hyderabad.
jshell> String empSerialized = g.toJson(e)
empSerialized ==> "{\n \"firstName\": \"Jai\",\n \"lastName\": \" ... ation\": \"Hyderabad\"\n}"</strong>立即学习“Java免费学习笔记(深入)”;
立即学习“Java免费学习笔记(深入)”;
在下面的代码片段中,我们可以创建一个Employee 对象的实例并显示结果。
<strong>jshell> System.out.println(empSerialized)
{
"firstName": "Jai",
"lastName": "Adithya",
"designation": "Content Developer",
"location": "Hyderabad"
}
jshell> Employee e1 = g.fromJson(empSerialized, Employee.class)
e1 ==> Name = Jai, Adithya | Job designation = Content D ... er | location = Hyderabad.</strong>以上就是在Java 9的JShell中如何导入gson库?的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号