我正在这里寻找一些解决方案,但没有找到我的问题的正确答案,所以我想问你。
我有带有一些简单属性的 POJO。以及另一个 POJO 的一个列表。
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;
}
在我的 Controller 类中,我使用 GSON 返回标准 POJO。
@RequestMapping(value="/fillStandard", method= RequestMethod.GET)
public @ResponseBody String getStandard(@RequestParam String id) {
Standard s = DAOFactory.getInstance().getStandardDAO().findById(id);
return new Gson().toJson(s);
}
问题是,我可以使用 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;
}
}
在这种情况下,你的 Json 看起来像:
{"id":"idValue", "title":"titleValue", "description":"descriptionValue", "interpretations":["id":"interpretationIdValue"]}
现在,您可以使用 jQuery
访问数据,如下所示:
function newStandard() {
$.get("standard/fillStandard.htm", {id:"fe86742b2024"}, function(data) {
alert(data.interpretations.title);
});
}
我希望这会有所帮助。
编辑:
我看到您的响应被转换为声明的方法参数类型,即 String
(如 here :16.3.3.2 支持的方法返回类型所述)。但您真正想要的是将您的 Standrad
POJO 转换为 JSON。我对 Spring 不是很熟悉,但正如我读过的 here (16.3.2.6 可生成的媒体类型)还有另一种可能更简单的解决方案。如果要返回 JSON 对象,则更改返回类型
将 getStandard
方法替换为 Standard
而不是 String
并将 products="application/json"
添加到您的 @ RequestMapping
注释。据我所读,这应该告诉 Spring
返回类型应该转换为 JSON。在这种情况下,您不需要使用 Gson
。
关于java - 使用 Spring Controller 返回的 jQuery 和 GSON 解析 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11191212/