如何序列化java.util.List< E>到Json使用Gson Library? [英] How to serialize a java.util.List<E> to Json using Gson Library?
问题描述
在我的servlet中,我有以下代码:
response.setContentType(application / json); // set json内容类型
PrintWriter out = response.getWriter();
EntityManagerFactory emf = Persistence.createEntityManagerFactory(webPU);
GpsJpaController gjc = new GpsJpaController(emf);
java.util.List< Gps> listGps = gjc.findGpsEntities(); //从DB
中检索GPS列表Gson gson = new Gson();
String json = gson.toJson(listGps); //转换列表< Gps>到json
out.println(json);
响应通过以下代码从ajax返回:
$。ajax({
类型:GET,
url:getInfoGeoActive,
dataType:'json',
错误:函数(xhr,ajaxOptions,thrownError){$(#content)。empty()。append(* Error from server cause:+ thrownError).addClass(error);},
成功:function(data){$(#content)。empty()。append(data);}
});
当我运行代码时,我仍然收到以下错误(来自ajax):
服务器原因导致的错误:内部Servlet错误
我也尝试在没有ajax的同一个java页面中显示序列化对象 List< Gps>
,如下所示:
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);
我得到这个错误:
<$ p $在线程main中的异常java.lang.StackOverflowError $ b在java.lang.StrictMath.floorOrCeil(StrictMath.java:355)处的$ b在java.lang.StrictMath.floor处
(StrictMath.java:340)
在java.lang.Math.floor(Math.java:424)
在sun.misc.FloatingDecimal.dtoa(FloatingDecimal.java:620)
at < init>(FloatingDecimal.java:459)$ b $在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)
at com.google.gson.Gson $ 4.write(Gson.java:255)
请帮助!
编辑:
这个问题的最终解决方案
StackOverflowException 由于无限递归而发生。您可能需要彻底检查代码是否有任何意外递归。
In my servlet I have the following code:
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);
response is returned from ajax with this code:
$.ajax({
type: "GET",
url: "getInfoGeoActive",
dataType: 'json',
error: function(xhr, ajaxOptions, thrownError){ $("#content").empty().append("* Error from server cause: "+thrownError).addClass("error"); } ,
success:function(data){$("#content").empty().append(data);}
});
when I run the code I still receive the following error(from ajax):
Error from server cause:Internal Servlet Error
I also tried to display the serialized object List<Gps>
in the same java page without ajax, as follows:
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);
I get this Error:
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)
Please Help!
EDIT: the final solution of this problem
StackOverflowException occurs due to infinite recursion. You may need to check the code thoroughly for any unintended recursion.
这篇关于如何序列化java.util.List< E>到Json使用Gson Library?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!