mercredi 25 février 2015

JQuery get Java ArrayList objects



I am trying to get the objects from array list in my jsp with JQuery and acces their parameters. Such as name, timestamp and etc. But how I am supposed to do it when it doesn't count this items in JQuery as the type of Object I need? Here is my 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);
}


And here is my 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>


And in this $.each I should be iterating trough the array I recived and this array is filled with Post obejects, so I need to get their properties for each of them so the question is: How I am supposed to get their properties?


Thank you in advance, hope someone explain me :)




Aucun commentaire:

Enregistrer un commentaire