0

I have written jsp code for generating json .But its not generating exact JsonArray. Its like

[{"item":"747"},,{"item":"1617"},]

instead of

[{"item":"747"},{"item":"1617"}]. 

below is the code

    JSONObject obj = new JSONObject();
    JSONArray ja = new JSONArray();
    obj.put("item", "747");
    ja.put(obj);
    obj = new JSONObject();
    obj.put("item", "1617");
    ja.put(obj);
    out.println(ja);
1
  • I tried your code, and works fine. what gson are you using? Commented Sep 3, 2014 at 10:18

1 Answer 1

2

I have tested your code by using json-simple-1.1.1.jar, worked fine there is no extra , came in output. while testing your code eclipse given one error message like put() method is undefined for the type JSONArray, so i replaced put with add like:

JSONObject obj = new JSONObject();
JSONArray ja = new JSONArray();
obj.put("item", "747");
ja.add(obj);
obj = new JSONObject();
obj.put("item", "1617");
ja.add(obj);
out.println(ja);

Also see Gson

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

Comments

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.