Java tutorial
//package com.java2s; import com.fasterxml.jackson.databind.ObjectMapper; import org.json.JSONArray; import org.json.JSONObject; public class Main { private static final ObjectMapper mapper = new ObjectMapper(); public static String asJson(Object value) throws Exception { if (value instanceof JSONArray || value instanceof JSONObject) { return value.toString(); } return mapper.writeValueAsString(value); } }