预计 FromJson BEGIN_OBJECT
FromJson Expected BEGIN_OBJECT
您好,我很难找到我的 Json 解析器的问题。
你好有一个对象学生(ID,姓名,年级)
这就是我在文档中写我的学生的方式:
public无效运行(){
System.out.println("Server get:" + value);
Gson gson = new Gson();
System.out.println("this record will be created in the source document");
String json = gson.toJson(value);
// System.out.println(json);
//2. Convert object to JSON string and save into a file directly
try (FileWriter writer = new FileWriter(File,true)) {
gson.toJson(value, writer);
writer.write("\n");
} catch (IOException e) {
e.printStackTrace();
}
}
在文档中我得到了
{"SID":"fd36ac24-4487-49aa-bdd0-40535b55d081","Name":"Marie","Major":"IT"}
这是一个很好的学生对象。现在我想再次将此文件信息放入学生对象中。这就是我尝试这样做的方式:
public Student Creation_Two() {
String fichier ="C:\Users\programming\Personne_source.txt";
Student s1 = new Student();
Gson gson = new Gson();
System.out.println("we try to parse the document with json to the object");
JsonReader reader = new JsonReader(new StringReader(fichier));
reader.setLenient(true);
System.out.println("reader value "+reader);
try
{
s1 = gson.fromJson(reader, Student.class);
System.out.println(s1 +" s1 have been serialized");
return s1;
}
catch (IllegalStateException | JsonSyntaxException e1)
{
System.out.println("error in getting the object");
e1.printStackTrace();
}
return s1;
}
但这没有用我有错误:预期 BEGIN_OBJECT 但在第 1 行第 2 列是 STRING。
这位同学class
public class Student {
private static final long serialVersionUID = 1L;
private String SID;
private String Name;
private String Major;
public Student(String SID, String Name, String Major)
{
this.setSID(SID);
this.setName(Name);
this.setMajor(Major);
}
public Student() {
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Student SID= " + SID + ", Name= " + Name + ", Major= " + Major + "";
}
//all the get set
试试这个:
Gson gson = new Gson();
System.out.println("this record will be created in the source document");
String fichier ="C:\Users\programming\Personne_source.txt";
byte[] encoded =Files.readAllBytes(Paths.get(fichier));
System.out.println("JSON: " + new String(encoded, "UTF-8"));
Student s1 = new Student();
try
{
s1 = gson.fromJson(new String(encoded, "UTF-8"), Student.class);
System.out.println(s1 +" s1 have been serialized");
}
catch (IllegalStateException | JsonSyntaxException e1)
{
System.out.println("error in getting the object");
e1.printStackTrace();
}
System.out.println(s1.toString());
好的,我试过你的代码,这就是我在控制台中得到的结果
JSON: {"SID":"577f484b-6ff8-4fbd-aa9d-1d7842874a03","Name":"Paul","Major":"IT"}
{"SID":"fd36ac24-4487-49aa-bdd0-40535b55d081","Name":"Marie","Major":"IT"}
error in getting the object
com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected EOF at line 2 column 1
Student SID= null, Name= null, Major= null
at com.google.gson.Gson.assertFullConsumption(Gson.java:772)
at com.google.gson.Gson.fromJson(Gson.java:762)
at com.google.gson.Gson.fromJson(Gson.java:710)
at com.google.gson.Gson.fromJson(Gson.java:682)
at Accepter_clients_Two.Creation_Two(Serveur_Two.java:275)
at Accepter_clients_Two.run(Serveur_Two.java:71)
at java.lang.Thread.run(Unknown Source)
类似的东西,我不明白,因为 JSON 输出输入很好。
但会不会是因为我的文档中有多个 JSON 对象?
您好,我很难找到我的 Json 解析器的问题。
你好有一个对象学生(ID,姓名,年级) 这就是我在文档中写我的学生的方式: public无效运行(){
System.out.println("Server get:" + value);
Gson gson = new Gson();
System.out.println("this record will be created in the source document");
String json = gson.toJson(value);
// System.out.println(json);
//2. Convert object to JSON string and save into a file directly
try (FileWriter writer = new FileWriter(File,true)) {
gson.toJson(value, writer);
writer.write("\n");
} catch (IOException e) {
e.printStackTrace();
}
}
在文档中我得到了 {"SID":"fd36ac24-4487-49aa-bdd0-40535b55d081","Name":"Marie","Major":"IT"}
这是一个很好的学生对象。现在我想再次将此文件信息放入学生对象中。这就是我尝试这样做的方式:
public Student Creation_Two() {
String fichier ="C:\Users\programming\Personne_source.txt";
Student s1 = new Student();
Gson gson = new Gson();
System.out.println("we try to parse the document with json to the object");
JsonReader reader = new JsonReader(new StringReader(fichier));
reader.setLenient(true);
System.out.println("reader value "+reader);
try
{
s1 = gson.fromJson(reader, Student.class);
System.out.println(s1 +" s1 have been serialized");
return s1;
}
catch (IllegalStateException | JsonSyntaxException e1)
{
System.out.println("error in getting the object");
e1.printStackTrace();
}
return s1;
}
但这没有用我有错误:预期 BEGIN_OBJECT 但在第 1 行第 2 列是 STRING。
这位同学class
public class Student {
private static final long serialVersionUID = 1L;
private String SID;
private String Name;
private String Major;
public Student(String SID, String Name, String Major)
{
this.setSID(SID);
this.setName(Name);
this.setMajor(Major);
}
public Student() {
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Student SID= " + SID + ", Name= " + Name + ", Major= " + Major + "";
}
//all the get set
试试这个:
Gson gson = new Gson();
System.out.println("this record will be created in the source document");
String fichier ="C:\Users\programming\Personne_source.txt";
byte[] encoded =Files.readAllBytes(Paths.get(fichier));
System.out.println("JSON: " + new String(encoded, "UTF-8"));
Student s1 = new Student();
try
{
s1 = gson.fromJson(new String(encoded, "UTF-8"), Student.class);
System.out.println(s1 +" s1 have been serialized");
}
catch (IllegalStateException | JsonSyntaxException e1)
{
System.out.println("error in getting the object");
e1.printStackTrace();
}
System.out.println(s1.toString());
好的,我试过你的代码,这就是我在控制台中得到的结果
JSON: {"SID":"577f484b-6ff8-4fbd-aa9d-1d7842874a03","Name":"Paul","Major":"IT"}
{"SID":"fd36ac24-4487-49aa-bdd0-40535b55d081","Name":"Marie","Major":"IT"}
error in getting the object
com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected EOF at line 2 column 1
Student SID= null, Name= null, Major= null
at com.google.gson.Gson.assertFullConsumption(Gson.java:772)
at com.google.gson.Gson.fromJson(Gson.java:762)
at com.google.gson.Gson.fromJson(Gson.java:710)
at com.google.gson.Gson.fromJson(Gson.java:682)
at Accepter_clients_Two.Creation_Two(Serveur_Two.java:275)
at Accepter_clients_Two.run(Serveur_Two.java:71)
at java.lang.Thread.run(Unknown Source)
类似的东西,我不明白,因为 JSON 输出输入很好。 但会不会是因为我的文档中有多个 JSON 对象?