public static String getLogList(String userID) { DBCursor cursor = collLog .find( (DBObject) JSON.parse( "{ $or : [ { userID : \"" + userID + "\"}, {users: \"" + userID + "\" }]}"), (DBObject) JSON.parse("{ _id : 0, nodeID : 0, users : 0 }")) .sort((DBObject) JSON.parse("{ time : -1 }")); JsonArray logArray = new JsonArray(); try { while (cursor.hasNext()) { String data = cursor.next().toString(); JsonObject jsonObj = new JsonObject(data); System.out.println(data); logArray.addObject(jsonObj); } } finally { cursor.close(); } JsonObject logList = new JsonObject(); logList.putArray("log", logArray); return logList.encode(); }
@Test public void testRetrieveArrayItemByIndex() { JsonArray arr = new JsonArray(); arr.addString("foo"); arr.addObject(new JsonObject().putString("bar", "baz")); arr.addString("bap"); assertEquals("baz", ((JsonObject) arr.get(1)).getString("bar")); }
public JsonArray add(Object value) { if (value instanceof JsonObject) { addObject((JsonObject) value); } else if (value instanceof JsonArray) { addArray((JsonArray) value); } else if (value instanceof String) { addString((String) value); } else if (value instanceof Number) { addNumber((Number) value); } else if (value instanceof Boolean) { addBoolean((Boolean) value); } else if (value instanceof byte[]) { addBinary((byte[]) value); } else { throw new VertxException("Cannot add objects of class " + value.getClass() + " to JsonArray"); } return this; }