FromJson预计BEGIN_OBJECT

时间:2016-12-01 11:33:18

标签: java file serialization gson streamwriter

您在查找我的Json解析器时遇到了什么问题。

你有一个对象学生(身份证,姓名,年级) 这就是我在我的文档中写下我的学生的方式:         public void run(){

        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”, “名称”: “玛丽”, “大调”: “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但是STRING在第1行第2列。

这是学生班

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 

2 个答案:

答案 0 :(得分:0)

试试这个:

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

答案 1 :(得分:0)

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

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 out输入是好的。 但这可能是因为我的文档中有多个JSON对象吗?