这个class的机制是什么? (JSON 解析)
What is the mechanism of this class? (JSON Parsing)
我试图理解这个简短的文件,但我无法理解它。你能解释一下它的用途吗?我认为它正在组织一个 JSON 文件?
package net.Whosebug;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class Program {
public String contents;
public String name;
public boolean visible;
public String fileHeader;
public Program() {
this.fileHeader = "";
this.name = "";
this.visible = false;
this.contents = "";
}
public Program(String metaJson) {
JsonParser parse = new JsonParser();
JsonObject entry = parse.parse(metaJson).getAsJsonObject();
this.fileHeader = entry.get("Filename").getAsString();
this.name = entry.get("displayName").getAsString();
this.visible = !entry.get("hidden").getAsBoolean();
this.contents = entry.get("data").getAsString();
}
}
请注意,此 class 使用的是 GSON 库。
此外,它以下列方式在另一个 class 中的哈希映射中使用:
public HashMap<String, theclassIdontget> directory;
我不知道这些信息是否有帮助,但不能太多...
感谢您的帮助!
正如您在标题中提到的,此 class 正在解析 json 字符串。
class 的以下部分是声明成员变量的地方:
public String contents;
public String name;
public boolean visible;
public String fileHeader;
这是 class 构造函数,它使用默认值初始化成员变量:
public Program() {
this.fileHeader = "";
this.name = "";
this.visible = false;
this.contents = "";
}
这是第二个构造函数,它接受一个字符串(即 json 内容),使用 JsonParser object 解析 json(检索请求的属性并存储这些值在成员变量 ).
public Program(String metaJson) {
JsonParser parse = new JsonParser();
JsonObject entry = parse.parse(metaJson).getAsJsonObject();
this.fileHeader = entry.get("Filename").getAsString();
this.name = entry.get("displayName").getAsString();
this.visible = !entry.get("hidden").getAsBoolean();
this.contents = entry.get("data").getAsString();
}
因此,当您使用 Program( string json )
构造函数创建 Program
object 时,例如:
Program myProgram = Program( jsonString )
您可以通过执行以下操作访问 json 属性 值:
myProgram.name
myProgram.contents
我试图理解这个简短的文件,但我无法理解它。你能解释一下它的用途吗?我认为它正在组织一个 JSON 文件?
package net.Whosebug;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class Program {
public String contents;
public String name;
public boolean visible;
public String fileHeader;
public Program() {
this.fileHeader = "";
this.name = "";
this.visible = false;
this.contents = "";
}
public Program(String metaJson) {
JsonParser parse = new JsonParser();
JsonObject entry = parse.parse(metaJson).getAsJsonObject();
this.fileHeader = entry.get("Filename").getAsString();
this.name = entry.get("displayName").getAsString();
this.visible = !entry.get("hidden").getAsBoolean();
this.contents = entry.get("data").getAsString();
}
}
请注意,此 class 使用的是 GSON 库。 此外,它以下列方式在另一个 class 中的哈希映射中使用:
public HashMap<String, theclassIdontget> directory;
我不知道这些信息是否有帮助,但不能太多... 感谢您的帮助!
正如您在标题中提到的,此 class 正在解析 json 字符串。
class 的以下部分是声明成员变量的地方:
public String contents;
public String name;
public boolean visible;
public String fileHeader;
这是 class 构造函数,它使用默认值初始化成员变量:
public Program() {
this.fileHeader = "";
this.name = "";
this.visible = false;
this.contents = "";
}
这是第二个构造函数,它接受一个字符串(即 json 内容),使用 JsonParser object 解析 json(检索请求的属性并存储这些值在成员变量 ).
public Program(String metaJson) {
JsonParser parse = new JsonParser();
JsonObject entry = parse.parse(metaJson).getAsJsonObject();
this.fileHeader = entry.get("Filename").getAsString();
this.name = entry.get("displayName").getAsString();
this.visible = !entry.get("hidden").getAsBoolean();
this.contents = entry.get("data").getAsString();
}
因此,当您使用 Program( string json )
构造函数创建 Program
object 时,例如:
Program myProgram = Program( jsonString )
您可以通过执行以下操作访问 json 属性 值:
myProgram.name
myProgram.contents