如何序列化java.util.List<;E>;使用Gson库创建Json?

如何序列化java.util.List<;E>;使用Gson库创建Json?,java,javascript,json,gson,Java,Javascript,Json,Gson,在我的servlet中,我有以下代码: response.setContentType("application/json");//set json content type PrintWriter out = response.getWriter(); EntityManagerFactory emf=Persistence.createEntityManagerFactory("webPU"); GpsJpaController gjc=new GpsJpaCo

在我的servlet中,我有以下代码:

    response.setContentType("application/json");//set json content type
    PrintWriter out = response.getWriter();

   EntityManagerFactory emf=Persistence.createEntityManagerFactory("webPU");
   GpsJpaController gjc=new GpsJpaController(emf);    
   java.util.List<Gps> listGps=gjc.findGpsEntities();//retrieve the list of GPS from DB 

   Gson gson=new Gson();
   String json=gson.toJson(listGps); //convert List<Gps> to json
   out.println(json);
当我运行代码时,仍然会收到以下错误(来自ajax):

我还尝试在没有ajax的同一java页面中显示序列化对象
列表
,如下所示:

EntityManagerFactory emf=Persistence.createEntityManagerFactory("JavaApplication21PU");
        GpsJpaController gjc=new GpsJpaController(emf);
        java.util.List<Gps> listGps=gjc.findGpsEntities();

        Gson gson=new Gson();
        String json=gson.toJson(listGps);

        System.out.println(json);
EntityManagerFactory emf=Persistence.createEntityManagerFactory(“JavaApplication21PU”);
GpsJpaController gjc=新的GpsJpaController(emf);
java.util.List listGps=gjc.findGpsEntities();
Gson Gson=新的Gson();
字符串json=gson.toJson(listGps);
System.out.println(json);
我得到这个错误:

Exception in thread "main" java.lang.StackOverflowError
    at java.lang.StrictMath.floorOrCeil(StrictMath.java:355)
    at java.lang.StrictMath.floor(StrictMath.java:340)
    at java.lang.Math.floor(Math.java:424)
    at sun.misc.FloatingDecimal.dtoa(FloatingDecimal.java:620)
    at sun.misc.FloatingDecimal.<init>(FloatingDecimal.java:459)
    at java.lang.Double.toString(Double.java:196)
    at java.lang.Double.toString(Double.java:633)
    at com.google.gson.stream.JsonWriter.value(JsonWriter.java:441)
    at com.google.gson.Gson$4.write(Gson.java:270)
    at com.google.gson.Gson$4.write(Gson.java:255)
线程“main”java.lang.StackOverflower中出现异常 位于java.lang.StrictMath.FloorCeil(StrictMath.java:355) 位于java.lang.StrictMath.floor(StrictMath.java:340) 在java.lang.Math.floor(Math.java:424) 位于sun.misc.FloatingDecimal.dtoa(FloatingDecimal.java:620) at sun.misc.FloatingDecimal.(FloatingDecimal.java:459) 位于java.lang.Double.toString(Double.java:196) 位于java.lang.Double.toString(Double.java:633) 位于com.google.gson.stream.JsonWriter.value(JsonWriter.java:441) 登录com.google.gson.gson$4.write(gson.java:270) 登录com.google.gson.gson$4.write(gson.java:255) 请帮忙

编辑
由于无限递归,StackOverflowException发生。您可能需要彻底检查代码是否存在任何意外递归。

看起来您的Gps对象包含对自身的引用。

考虑到这是在Gson转换为json时发生的,我想@ahanin是对的。全球定位系统可能是罪魁祸首。
EntityManagerFactory emf=Persistence.createEntityManagerFactory("JavaApplication21PU");
        GpsJpaController gjc=new GpsJpaController(emf);
        java.util.List<Gps> listGps=gjc.findGpsEntities();

        Gson gson=new Gson();
        String json=gson.toJson(listGps);

        System.out.println(json);
Exception in thread "main" java.lang.StackOverflowError
    at java.lang.StrictMath.floorOrCeil(StrictMath.java:355)
    at java.lang.StrictMath.floor(StrictMath.java:340)
    at java.lang.Math.floor(Math.java:424)
    at sun.misc.FloatingDecimal.dtoa(FloatingDecimal.java:620)
    at sun.misc.FloatingDecimal.<init>(FloatingDecimal.java:459)
    at java.lang.Double.toString(Double.java:196)
    at java.lang.Double.toString(Double.java:633)
    at com.google.gson.stream.JsonWriter.value(JsonWriter.java:441)
    at com.google.gson.Gson$4.write(Gson.java:270)
    at com.google.gson.Gson$4.write(Gson.java:255)