继续获取一个空的JSON对象

时间:2015-03-23 22:07:10

标签: java jquery json

您好我是JSON的新手,我一直在尝试将我的List放入JSONArray中,以便稍后可以将它与JQuery一起使用并将其包含在网站上,但它会不断返回一个空数组。我正在使用Java EE并编写了一个命名查询,我不太确定命名查询是它背后的小偷。这很可能是我希望的合适代码。

Review.class中的命名查询:

@NamedQueries({

@NamedQuery(name="Review.findByTitleOrName", query = "SELECT r FROM Review r WHERE r.artist.artistNr = (SELECT a.artistNr FROM Artist a WHERE a.artistName Like :A) OR r.track.trackID = (SELECT t.trackID FROM Track t WHERE t.trackTitle LIKE :A)")
})

EAO中用于审核的方法:

public List<Review> getReviewsByTitleOrName(String searchParameter) {
        TypedQuery<Review> tq = em.createNamedQuery("Review.findByTitleOrName", Review.class);

        tq.setParameter("A", searchParameter); 

        List<Review> reviewList = tq.getResultList();
        return reviewList;
    } 

Servlet代码:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json; charset=utf-8");

        JsonArrayBuilder arrayOfJsonReviews = Json.createArrayBuilder();

        String searchParameter = request.getParameter("ajax_searchParameter");
        List<Review> reviews = facade.getReviewsByTitleOrName(searchParameter);


        for(Review r : reviews){
            JsonObjectBuilder review = Json.createObjectBuilder();
            review.add("date", r.getDate());
            review.add("comment", r.getComments());
            review.add("rating", r.getRating());
            review.add("user", r.getUserName());
            arrayOfJsonReviews.add(review);
        }

        response.getWriter().write(arrayOfJsonReviews.build().toString());
    }

最后但并非最不重要的是JQuery:

$(document).ready(function() {
    $(".SearchBtn").click(function() {
        console.log("Clicked button");
        $.getJSON("http://localhost:8080/DynamicDolphinProject/DolphinServlet", function(data){
            console.log(data);
            //placeReviewDat(data);
            console.log("Should have data");
        });
    });

    //function placeReviewData(reviews){

    //}
})

0 个答案:

没有答案