Java 使用Spring控制器返回的jQuery和GSON解析JSON对象
我在这里寻找一些解决方案,但我没有找到任何正确的答案,所以我想问你 我有一些简单属性的POJO。还有另一个POJO的列表Java 使用Spring控制器返回的jQuery和GSON解析JSON对象,java,spring-mvc,gson,Java,Spring Mvc,Gson,我在这里寻找一些解决方案,但我没有找到任何正确的答案,所以我想问你 我有一些简单属性的POJO。还有另一个POJO的列表 public class Standard implements Serializable { private String id; private String title; private String description; private Set<Interpretation> interpretations = new
public class Standard implements Serializable {
private String id;
private String title;
private String description;
private Set<Interpretation> interpretations = new LinkedHashSet<Interpretation>();
}
public class Interpretation implements Serializable {
private String id;
private String title;
private String description;
}
问题是,我是否能够使用jQuery获得标准POJO中的解释列表?比如:
function newStandard() {
$.get("standard/fillStandard.htm", {id:"fe86742b2024"}, function(data) {
alert(data.interpretations[0].title);
});
}
非常感谢
编辑:
多亏了@Atticus,我的问题终于有了解决办法。希望它能帮助别人
@RequestMapping(value="/fillStandard", method= RequestMethod.GET, produces="application/json")
public @ResponseBody Standard getStandard(@RequestParam String id) {
Standard s = DAOFactory.getInstance().getStandardDAO().findById(id);
return s;
}
使用@ResponseBody
可以返回整个POJO,但需要将products=“application/json”
添加到@RequestMapping
注释中。然后,您将能够像我想象的那样,在jQuery中捕获返回的JSON对象
function newStandard() {
$.get("standard/fillStandard.htm", {id:"idOfStandard"}, function(data) {
alert(data.id); //Standard id
alert(data.interpretations[0].title); //id of Interpretation on first place in array
});
您必须创建并注册自定义序列化程序 事情是这样的:
//You create your builder that registers your custom serializer with the class you want to serialize
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Standard.class, new StandardSerializer());
//Then you create your Gson object
Gson gson = builder.create();
//Then you pass your object to the gson like
Standard s = DAOFactory.getInstance().getStandardDAO().findById(id);
gson.toJson(s);
public class StandardSerializer implements JsonSerializer<Standard>{
@Override
public JsonElement serialize(Standard src, Type typeOfSrc,
JsonSerializationContext context) {
JsonObject obj = new JsonObject();
//You put your simple objects in like this
obj.add("id",new JsonPrimitive(src.getId()));
//You put your complex objects in like this
JsonObject interpretations = new JsonObject();
//Here you need to parse your LinkedHashSet object and set up the values.
//For the sake of simplicity I just access the properties (even though I know this would not compile)
interpretations.add("title", src.getInterpretation().getTitle());
obj.add("interpretations", interpretations);
return obj;
}
}
function newStandard() {
$.get("standard/fillStandard.htm", {id:"fe86742b2024"}, function(data) {
alert(data.interpretations.title);
});
}
您的序列化程序如下所示:
//You create your builder that registers your custom serializer with the class you want to serialize
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Standard.class, new StandardSerializer());
//Then you create your Gson object
Gson gson = builder.create();
//Then you pass your object to the gson like
Standard s = DAOFactory.getInstance().getStandardDAO().findById(id);
gson.toJson(s);
public class StandardSerializer implements JsonSerializer<Standard>{
@Override
public JsonElement serialize(Standard src, Type typeOfSrc,
JsonSerializationContext context) {
JsonObject obj = new JsonObject();
//You put your simple objects in like this
obj.add("id",new JsonPrimitive(src.getId()));
//You put your complex objects in like this
JsonObject interpretations = new JsonObject();
//Here you need to parse your LinkedHashSet object and set up the values.
//For the sake of simplicity I just access the properties (even though I know this would not compile)
interpretations.add("title", src.getInterpretation().getTitle());
obj.add("interpretations", interpretations);
return obj;
}
}
function newStandard() {
$.get("standard/fillStandard.htm", {id:"fe86742b2024"}, function(data) {
alert(data.interpretations.title);
});
}
现在,您可以使用jQuery
访问数据,如下所示:
//You create your builder that registers your custom serializer with the class you want to serialize
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Standard.class, new StandardSerializer());
//Then you create your Gson object
Gson gson = builder.create();
//Then you pass your object to the gson like
Standard s = DAOFactory.getInstance().getStandardDAO().findById(id);
gson.toJson(s);
public class StandardSerializer implements JsonSerializer<Standard>{
@Override
public JsonElement serialize(Standard src, Type typeOfSrc,
JsonSerializationContext context) {
JsonObject obj = new JsonObject();
//You put your simple objects in like this
obj.add("id",new JsonPrimitive(src.getId()));
//You put your complex objects in like this
JsonObject interpretations = new JsonObject();
//Here you need to parse your LinkedHashSet object and set up the values.
//For the sake of simplicity I just access the properties (even though I know this would not compile)
interpretations.add("title", src.getInterpretation().getTitle());
obj.add("interpretations", interpretations);
return obj;
}
}
function newStandard() {
$.get("standard/fillStandard.htm", {id:"fe86742b2024"}, function(data) {
alert(data.interpretations.title);
});
}
我希望这有帮助
编辑:
我看到您的响应被转换为声明的方法参数类型,即String
(如上所述:16.3.3.2支持的方法返回类型)。但您真正想要的是将Standrad
POJO转换为JSON。我对Spring不是很熟悉,但正如我所读到的(16.3.2.6可生产媒体类型),还有另一个可能更简单的解决方案。如果要返回JSON对象,请更改
将
getStandard
方法改为Standard
而不是String
,并将products=“application/json”
添加到@RequestMapping
注释中。据我所知,这应该告诉Spring返回类型应该转换为JSON。在这种情况下,您不需要使用Gson
我已经编辑了我的答案,以包含jQuery代码。您必须创建一个JSON对象,其中包含您请求的数据。好的,这就是您发送数据(在服务器端)以便能够在客户端接收所需内容的方式。感谢您的回复:)我使用了您的代码,现在在jQuery alert(data.toString)中返回{“handler”:{“interfaces”:[{}],“constructed”:“true”,“persistentClass”:{},“overridesEquals”:“true”,“entityName”:“com.web.model.Standard”,“id”:“fe86742b2024”,“目标”:{“id”:“fe86742b2024”,“标题”:“fe86742b2024”,“说明”:“说明”,“解释”:{“id”:“5f5c9dc31119”,“标题”:“5.2.4”,“说明”:“说明”},“初始化”:true},“其他”:false,“解释”:[],“评论”:[]}我无法从中得到任何信息。data.id和data.explations.id都没有?我做错了什么?出于某种原因,您的数据被放入JSON答案的目标字段,其他属性也被序列化。如果不看一下代码,我就说不出问题出在哪里。您能把它发送给我吗(使用我个人资料页面上的电子邮件地址)?没问题。只需在我的评论后或我的回答下单击我的名字,你就会看到我的个人资料:)是的,它工作起来很有魅力!我试图使用@ResponseBody
(顺便说一句,很棒的Spring工具)返回整个POJO标准,但没有products=“application/json”“
在@RequestMapping
中,没有任何内容发送回my.get jQuery方法。因此,我开始尝试返回字符串或HashMap(因为默认情况下支持这种类型)。现在我既不需要GSON也不需要其他任何东西。感谢上帝,也感谢你:D