【问题标题】:FromJson Expected BEGIN_OBJECTFromJson 预期 BEGIN_OBJECT
【发布时间】:2017-04-16 00:44:09
【问题描述】:

您好,在查找我的 Json 解析器出了什么问题时遇到了一些麻烦。

你好有一个对象学生(ID,姓名,年级) 这就是我在我的文档中写我的学生的方式: 公共无效运行(){

        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。

这里是学生课

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 

【问题讨论】:

  • 你能粘贴 Student 类吗?
  • 我现在编辑了问题以放置课程。
  • 不要初始化s1,也可以说Student s1 = gson.fromJson(reader, Student.class);JsonReader reader = new JsonReader(new StringReader(fichier)); 你使用了错误的阅读器,读取的是字符串,你需要一个文件阅读器。
  • @thor 你试过我告诉你的选项了吗?
  • @cralfaro 是的,我对其进行了测试,但仍然是错误,我在旁边打印了控制台输出。但我认为我必须修改一些东西,因为我的文件中可以有随机数量的学生,我必须序列化只有 n 个学生

标签: java file serialization gson streamwriter


【解决方案1】:

试试这个:

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());

【讨论】:

    【解决方案2】:

    好的,我试过你的代码,这就是我在控制台中得到的

    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 对象吗?

    【讨论】:

    • 我让它使用完全相同的代码。请验证您的 txt 文件末尾没有任何换行符或末尾没有空格
    • @cralfaro 是的,我现在找到了方法。我必须重新编写对象并获得大量学生。
    • 太好了,我希望代码可以帮助您找到解决方案
    猜你喜欢
    • 2018-12-14
    • 1970-01-01
    • 1970-01-01
    • 2014-08-02
    • 2015-07-24
    • 2019-05-10
    • 2015-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多