Search code examples
javajsonjson-lib

How do I put more than one JSON instance in a single JSON object using net.sf.json.JSONObject library


 //create the JSON Object to pass to the client
 JSONObject object=new JSONObject();

 //one instance
 object.put("name","something2");
 object.put("status", "up");

 //second instance
 object.put("name", "something2");
 object.put("status", "down");

 String json = object.toString();  

 response.getOutputStream().print(json);

 System.out.println("JSON Contents: "+json);

Desired output: {name: something1, status: up}, {name: something2, status: down}... etc


Solution

  • You need to have JSONArray :

    JSONArray jsonarray = new JSONArray(); jsonarray.add(object)...