Looking for android Answers? Try Ask4KnowledgeBase
Looking for android Keywords? Try Ask4Keywords

Android Create nested JSON object


To produce nested JSON object, you need to simply add one JSON object to another:

JSONObject mainObject = new JSONObject();            // Host object
JSONObject requestObject = new JSONObject();         // Included object

try {
    requestObject.put("lastname", lastname);
    requestObject.put("phone", phone);
    requestObject.put("latitude", lat);
    requestObject.put("longitude", lon);
    requestObject.put("theme", theme);
    requestObject.put("text", message);

    mainObject.put("claim", requestObject);
} catch (JSONException e) {
    return "JSON Error";

Now mainObject contains a key called claim with the whole requestObject as a value.