Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用gson和JsonWriter反序列化对象数组_Java_Json_Gson - Fatal编程技术网

Java 如何使用gson和JsonWriter反序列化对象数组

Java 如何使用gson和JsonWriter反序列化对象数组,java,json,gson,Java,Json,Gson,我有以下课程: public class MeetingCenter { private String name; private List<MeetingRoom> meetingRoomList; } public class MeetingRoom { private MeetingCenter meetingCenter; private String code; private String name; private Li

我有以下课程:

public class MeetingCenter {
    private String name;
    private List<MeetingRoom> meetingRoomList;
}

public class MeetingRoom {
    private MeetingCenter meetingCenter;
    private String code;
    private String name;
    private List<Reservation> reservationList;
}

public class Reservation {
    private MeetingRoom meetingRoom;
    private String owner;
}

您的对象都有对其父对象的引用

GSON查看了一个
会议中心
,然后尝试序列化其子
会议室
s。
MeetingRoom
s将引用返回到
MeetingCenter
,因此GSON会不停地循环,直到堆栈溢出为止

要解决此问题,可以确保只暴露子对象而不暴露父对象。已经有很多问题表明了这一点。看

例如,您的
预订可能如下所示:

class Reservation {
    MeetingRoom meetingRoom;
    @Expose
    String owner;
}
剩下的就交给你了


另外,当您还没有启动数组时,您有一个对
writer.endArray()
的方法调用。拆下那条线

writer.beginObject(); // document start
writer.name("schema").value("PLUS4U.EBC.MCS.MeetingRoom_Schedule_1.0");
writer.name("uri").value("ues:UCL-BT:UCL.INF/DEMO_REZERVACE:EBC.MCS.DEMO/MR001/SCHEDULE");
writer.name("data").value(data);

//writer.endArray(); removed
writer.endObject(); // document end
writer.close();

@Expose是避免stackoverflow异常的解决方案,但是

writer.name("data").value(data);
无效,因为数据将使用转义字符进行充实。例如,您可以在数据字段中使用

"data": "{\"name\": \"center 1\" ... }"
因此,在反序列化阶段可能会出现问题

我的实现为MeetingCenter类提出了一个容器类,可以在其中配置模式和URI

/** Container class configures the schema and URI */
public class Container {
    @Expose
    private String schema;
    @Expose
    private String uri;
    @Expose
    private List<MeetingCenter> data;
}

public class Reservation {
    private MeetingRoom meetingRoom;
    @Expose
    private String owner;
}

public class MeetingRoom {
    private MeetingCenter meetingCenter;
    @Expose
    private String code;
    @Expose
    private String name;
    @Expose
    private List<Reservation> reservationList;
}

public class MeetingCenter {
    @Expose
    private String name;
    @Expose
    private List<MeetingRoom> meetingRoomList;
}

public class Main {
    public static void main(String[] args){
        Container container = meetingCenterInitialization();

        GsonBuilder builder = new GsonBuilder();
        builder.setPrettyPrinting();
        // it is necessary to avoid stackoverflow
        builder.excludeFieldsWithoutExposeAnnotation();

        Gson gson = builder.create();

        String jsonString = gson.toJson(container);
        System.out.println(jsonString);


        Container container1 = gson.fromJson(jsonString, Container.class);
        System.out.println("\n\n\n\n" + container1.getData().get(0).getName());
    }
}

您能打印检索到的异常吗?在没有看到异常的情况下,我猜问题出在对象结构的循环中:
MeetingRoom->Reservation->MeetingRoom
。你需要以某种方式打破这种结构。例如,您可能不想在reservation中引用MeetingRoom,而只想序列化MeetingRoom的代码。我明白了,所以我不能使用gson,只要我不想更改它们实现reservation类的方式。您可以在字段上添加一些gson注释,或者如果无法修改源代码(例如,如果它来自一个库),您可以为Reservation类实现一个序列化程序that@NeplatnyUdaj我明白了。然后有两个问题。编辑了我的答案。
"data": "{\"name\": \"center 1\" ... }"
/** Container class configures the schema and URI */
public class Container {
    @Expose
    private String schema;
    @Expose
    private String uri;
    @Expose
    private List<MeetingCenter> data;
}

public class Reservation {
    private MeetingRoom meetingRoom;
    @Expose
    private String owner;
}

public class MeetingRoom {
    private MeetingCenter meetingCenter;
    @Expose
    private String code;
    @Expose
    private String name;
    @Expose
    private List<Reservation> reservationList;
}

public class MeetingCenter {
    @Expose
    private String name;
    @Expose
    private List<MeetingRoom> meetingRoomList;
}

public class Main {
    public static void main(String[] args){
        Container container = meetingCenterInitialization();

        GsonBuilder builder = new GsonBuilder();
        builder.setPrettyPrinting();
        // it is necessary to avoid stackoverflow
        builder.excludeFieldsWithoutExposeAnnotation();

        Gson gson = builder.create();

        String jsonString = gson.toJson(container);
        System.out.println(jsonString);


        Container container1 = gson.fromJson(jsonString, Container.class);
        System.out.println("\n\n\n\n" + container1.getData().get(0).getName());
    }
}
{
  "schema": "PLUS4U.EBC.MCS.MeetingRoom_Schedule_1.0",
  "uri": "ues:UCL-BT:UCL.INF/DEMO_REZERVACE:EBC.MCS.DEMO/MR001/SCHEDULE",
  "data": [
    {
      "name": "center name",
      "meetingRoomList": [
        {
          "code": "room 1",

         ...