0

This is my servlet code. I want add my json object values into a json array.I used add method for that but I'm getting an error. How to add that object to my array? Is there any mistake in my code?

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
     throws ServletException, IOException {

    try {
        Session ses = HibernateSession.getSession();
        Criteria cr1 = HibernateSession.createCriteria(ses, Product.class);
        cr1.add(Restrictions.eq("Status", "Active"));
        List<Product> plist = cr1.list();

        JSONArray ja1 = new JSONArray();
        for (Product product : plist) {
            JSONObject jo1 = new JSONObject();
            jo1.put("image", product.getProductImages());
            jo1.put("name", product.getName());
            jo1.put("price", product.getPrice());

            ja1.add(jo1);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

enter image description here

8
  • what kind of error? Commented Jun 5, 2018 at 12:56
  • not error. cannot use add Commented Jun 5, 2018 at 12:57
  • maybe ja1 vs js1 Commented Jun 5, 2018 at 12:59
  • image upload.cannot find symbol Commented Jun 5, 2018 at 13:01
  • 2
    maybe use ja1.put() (assuming you are using org.json.JSONArray) Commented Jun 5, 2018 at 13:03

2 Answers 2

2

Try to use put() instead of add() ;)

Sign up to request clarification or add additional context in comments.

Comments

1

This depends on the libraries which you are using.

If you are using org.json.JSONArray then it should be put() but if you are using org.json.simple.JSONArray it's add().


I bet you are using org.json.JSONArray. So try,

ja1.put(jo1);

1 Comment

what is the different between org.json. & org.json.simple.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.