Java IntelliJ IDE显示Gson成功地将字符串转换为对象,但当我导出到Jar时,它会显示;应为BEGIN_对象,但为字符串
我有两个文本文件:PatientForms.txt和MasterCodeList.txt 每个文件的每一行都包含一个JSON字符串。例如,我使用以下方法读入文件的内容并将其存储在SortedMap中 以下是我的ServiceCode.java代码:Java IntelliJ IDE显示Gson成功地将字符串转换为对象,但当我导出到Jar时,它会显示;应为BEGIN_对象,但为字符串,java,intellij-idea,gson,Java,Intellij Idea,Gson,我有两个文本文件:PatientForms.txt和MasterCodeList.txt 每个文件的每一行都包含一个JSON字符串。例如,我使用以下方法读入文件的内容并将其存储在SortedMap中 以下是我的ServiceCode.java代码: public class ServiceCode { //class variables @SerializedName("code") private String code; @SerializedName("description") pr
public class ServiceCode {
//class variables
@SerializedName("code")
private String code;
@SerializedName("description")
private String description;
@SerializedName("cost")
private double fullRetailPrice;
//constructor: default - creates an empty code that can be modified by user
public ServiceCode() {
setCode("Empty");
setDescription("Empty");
setFullRetailPrice(0.0);
}
//constructor: non-default - creates a code with values passed to it for code, description, and fullRetailPrice
public ServiceCode(String code, String description, double fullRetailPrice) {
setCode(code);
setDescription(description);
setFullRetailPrice(fullRetailPrice);
}
//setters
public void setCode(String code) { this.code = code; }
public void setDescription(String description) { this.description = description; }
public void setFullRetailPrice(double fullRetailPrice) { this.fullRetailPrice = fullRetailPrice; }
//getters
public String getCode() { return code; }
public String getDescription() { return description; }
public double getFullRetailPrice() { return fullRetailPrice; }
以下是我读取文件并将其放置在地图中的方法:
public class CodeFileHandler {
//static final File dir = new File(".");
static final Gson gson = new Gson();
public SortedMap<String, ServiceCode> loadCodeList() {
SortedMap<String, ServiceCode> map = new TreeMap<>();
List<String> codeStrings = new ArrayList<>();
try {
String loc = "MasterCodeList.txt";
BufferedReader br = new BufferedReader(new FileReader(loc));
for(String line; (line = br.readLine()) != null; ) {
codeStrings.add(line);
System.out.println(line);
}
// line is not visible here.
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
for (String s : codeStrings) {
//used to show that string is a valid JSON format
System.out.println(s);
ServiceCode c = gson.fromJson(s, ServiceCode.class);
map.put(c.getCode(), c);
}
return map;
}
当我从jar文件运行它时,我得到如下结果:
F:\BLCHC\u MCv1\out\artifacts\BLCHC\u MCv1\u jar>java-jar BLCHC\u MCv1.jar
ï»{“代码”:“D0120”,“说明”:“定期口头评估确定pt”,“成本”:39.0}
线程“AWT-EventQueue-0”com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:应为BEGIN_对象,但在第1行第1列路径处为字符串$
位于com.google.gson.internal.bind.ReflectTypeAdapterFactory$Adapter.read(ReflectTypeAdapterFactory.java:224)
位于com.google.gson.gson.fromJson(gson.java:887)
位于com.google.gson.gson.fromJson(gson.java:852)
位于com.google.gson.gson.fromJson(gson.java:801)
位于com.google.gson.gson.fromJson(gson.java:773)
位于com.olsen.blchc.CodeFileHandler.loadCodeList(CodeFileHandler.java:39)
在过去的几天里,我所看到的所有来源都指向在开始时用“代替”{读取的字符串。事实并非如此
有人知道为什么会发生这种情况吗?当我访问PatientForms.txt时,从不同的文件加载它没有问题,即使它是从jar运行的
更新!
我将loadCodeList方法更改为:
public void loadCodeList() {
try {
String loc = "MasterCodeList.txt";
BufferedReader br = new BufferedReader(new FileReader(loc));
for(String line; (line = br.readLine()) != null; ) {
ServiceCode s = gson.fromJson(line, ServiceCode.class);
codeMap.put(s.getCode(), s);
}
// line is not visible here.
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
我的代码读取每一行,并使用gson.fromJson(string,ServiceCode);
将其从JSON格式的字符串转换为ServiceCode对象,其中string是从文件中读取的JSON字符串
IntelliJ IDE的输出
在IDE中,我可以毫无故障地运行一切,并访问SortedMap中的ServiceCode对象
JAR文件输出
我没有正确打包我的jar文件吗?我将Gson jar包含在我的jar目录中。花了很多时间,我终于找到了一种没有错误的方法!我正在阅读如何保存并加载我的映射(保存在JSON文件中,具有完整的格式和所有内容)我发现:。现在,当我从.jar文件和IDE中运行它时,它可以工作。这个异常说明json文件以字符串开头。因此,请更改为
string s=gson.fromJson(s,string.class);
我尝试过,但它只是给了我一个错误,说:没有类型变量的实例存在,以便字符串符合ServiceCode接口变量T具有不兼容的边界:相等约束:字符串上限:对象,ServiceCode
public void loadCodeList() {
try {
String loc = "MasterCodeList.txt";
BufferedReader br = new BufferedReader(new FileReader(loc));
for(String line; (line = br.readLine()) != null; ) {
ServiceCode s = gson.fromJson(line, ServiceCode.class);
codeMap.put(s.getCode(), s);
}
// line is not visible here.
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}