Code example for JsonObject

Methods: toStringaddProperty

0
					jsonResponse(status < 300,
						new String[]{"message"}, new String[]{message}, directTo));
	} 
	 
	public static String jsonResponse(boolean success, String directTo) {
		JsonObject resJson = new JsonObject();
		resJson.addProperty("success", success);
		if(directTo != null)
			resJson.addProperty("directTo", directTo);
		return resJson.toString();
	} 
	 
	public static String jsonResponse(boolean success, String[] dataNames, Object[] data, String directTo) {
		if(dataNames == null || data == null || dataNames.length != data.length)
			throw new IllegalArgumentException("Data names or data is null or mismatching");
		JsonObject resJson = new JsonObject();
		resJson.addProperty("success", success);
		Gson gson = new Gson();
		for(int i = 0; i < data.length; i++)
			resJson.add(dataNames[i], gson.toJsonTree(data[i]));