JQuery 获取 Java 个 ArrayList 对象
JQuery get Java ArrayList objects
我正在尝试使用 JQuery 从我的 jsp 中的数组列表中获取对象并访问它们的参数。例如名称、时间戳等。但是当它不将 JQuery 中的这些项目计为我需要的对象类型时,我应该怎么做呢?
这是我的 servlet:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
long id = (Long) session.getAttribute("lastPost");
ArrayList<Post> listOfPosts = PostRegistry.getInstance().getPostList(id);
Gson gson = new Gson();
JsonElement element = gson.toJsonTree(listOfPosts, new TypeToken<List<Post>>() {
}.getType());
JsonArray jsonArray = element.getAsJsonArray();
response.setContentType("application/json");
response.getWriter().print(jsonArray);
}
这是我的 JQuery :
<script>
$(document).ready(function() {
$('#timeline').hide();
$('#LoadMore').click(function() {
$.get('LoadMore', function(responseJson) {
// Parse the json result on somediv
$.each(responseJson, function(index, item) {
});
});
});
});
</script>
在这个 $.each 中,我应该遍历我收到的数组,这个数组充满了 Post 个对象,所以我需要为每个对象获取它们的属性,所以问题是:如何我应该得到他们的财产?
提前谢谢你,希望有人解释我:)
此站点上有与您类似的问题,但一种可能的技术是将本机 Java 对象而不是 JSON 传递给页面,并使用 EL 创建一个 for 循环遍历您的 arrayList,提取您的值,然后存储在隐藏的输入元素中,稍后您的 JQuery/JavaScripts 函数可以访问这些元素,即
<c:forEach var='post' items=${listOfPosts} >
<c:set var="attribute1" value=${post.attribute1} />
<c:set var="attribute2" value=${post.attribute2} />
<input type="hidden" id="postXXXattribute1" value="${attribute1}" />
<input type="hidden" id="postXXXattribute2" value="${attribute2}" />
</c:forEach>
我省略了一些有关如何将值写入 HTML 的细节,以便可以从 JQuery 或 JavaScript 轻松访问它们,但希望您得到我想告诉你的。如果没有,请向我询问更多详情...
最重要的是,您应该使用 jQuery.getJSON()
而不是常规的 jQuery.get()
。
$.getJSON('LoadMore', function(data) {
$.each(data, function(index, item) {
var postTitle = item.title; // example: access title property
});
});
如果您似乎没有遍历 javascript 中的 Post
对象列表,请使用 jQuery.get()
进行调试以通过弹出来发现实际的 JSON 结构在您的浏览器中创建一个消息框:
alert(responseJson);
附带说明一下,使用 toJsonTree()
没有意义;你应该使用 toJson()
。此外,调用 getAsJsonArray()
可能会将您的结果嵌套在冗余数组中。此外,将输出直接通过管道传输到您的响应 Writer
会更有效。像这样:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Long id = (Long)request.getSession().getAttribute("lastPost");
List<Post> listOfPosts = PostRegistry.getInstance().getPostList(id);
response.setContentType("application/json");
new Gson().toJson(listOfPosts,
new TypeToken<List<Post>>(){}.getType(),
response.getWriter());
}
我正在尝试使用 JQuery 从我的 jsp 中的数组列表中获取对象并访问它们的参数。例如名称、时间戳等。但是当它不将 JQuery 中的这些项目计为我需要的对象类型时,我应该怎么做呢? 这是我的 servlet:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
long id = (Long) session.getAttribute("lastPost");
ArrayList<Post> listOfPosts = PostRegistry.getInstance().getPostList(id);
Gson gson = new Gson();
JsonElement element = gson.toJsonTree(listOfPosts, new TypeToken<List<Post>>() {
}.getType());
JsonArray jsonArray = element.getAsJsonArray();
response.setContentType("application/json");
response.getWriter().print(jsonArray);
}
这是我的 JQuery :
<script>
$(document).ready(function() {
$('#timeline').hide();
$('#LoadMore').click(function() {
$.get('LoadMore', function(responseJson) {
// Parse the json result on somediv
$.each(responseJson, function(index, item) {
});
});
});
});
</script>
在这个 $.each 中,我应该遍历我收到的数组,这个数组充满了 Post 个对象,所以我需要为每个对象获取它们的属性,所以问题是:如何我应该得到他们的财产?
提前谢谢你,希望有人解释我:)
此站点上有与您类似的问题,但一种可能的技术是将本机 Java 对象而不是 JSON 传递给页面,并使用 EL 创建一个 for 循环遍历您的 arrayList,提取您的值,然后存储在隐藏的输入元素中,稍后您的 JQuery/JavaScripts 函数可以访问这些元素,即
<c:forEach var='post' items=${listOfPosts} >
<c:set var="attribute1" value=${post.attribute1} />
<c:set var="attribute2" value=${post.attribute2} />
<input type="hidden" id="postXXXattribute1" value="${attribute1}" />
<input type="hidden" id="postXXXattribute2" value="${attribute2}" />
</c:forEach>
我省略了一些有关如何将值写入 HTML 的细节,以便可以从 JQuery 或 JavaScript 轻松访问它们,但希望您得到我想告诉你的。如果没有,请向我询问更多详情...
最重要的是,您应该使用 jQuery.getJSON()
而不是常规的 jQuery.get()
。
$.getJSON('LoadMore', function(data) {
$.each(data, function(index, item) {
var postTitle = item.title; // example: access title property
});
});
如果您似乎没有遍历 javascript 中的 Post
对象列表,请使用 jQuery.get()
进行调试以通过弹出来发现实际的 JSON 结构在您的浏览器中创建一个消息框:
alert(responseJson);
附带说明一下,使用 toJsonTree()
没有意义;你应该使用 toJson()
。此外,调用 getAsJsonArray()
可能会将您的结果嵌套在冗余数组中。此外,将输出直接通过管道传输到您的响应 Writer
会更有效。像这样:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Long id = (Long)request.getSession().getAttribute("lastPost");
List<Post> listOfPosts = PostRegistry.getInstance().getPostList(id);
response.setContentType("application/json");
new Gson().toJson(listOfPosts,
new TypeToken<List<Post>>(){}.getType(),
response.getWriter());
}