Probably what you need is different from what you think is necessary;
You must have a separate User object to store all properties, such as name, age, etc. And then this object should have a method providing you with a Json representation of the object ...
You can check the code below:
import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; public class User { String name; Integer age; public User(String name, Integer age) { this.name = name; this.age = age; } public JSONObject toJson() { try { JSONObject json = new JSONObject(); json.put("name", name); json.put("age", age); return json; } catch (JSONException e) { e.printStackTrace(); return null; } } public static void main(String[] args) { User lamis = new User("lamis", 23); System.out.println(lamis.toJson()); } }
source share