How to nest objects when building JSON with JSONObject

If you're asking how you'd put the nested params object in there, you'd probably do:

JSONObject params = new JSONObject();
params.put("media", "music");

obj.put("params", params);

To use an array (per your comments below), you'd do something like this:

JSONArray properties = new JSONArray();
properties.put("resume");
properties.put("genre");
properties.put("studio");
...

JSONObject params = new JSONObject();
params.put("properties", properties);

obj.put("params", params);

JSONOjbect obj = new JSONObject().put("jsonrpc", "2.0")
    .put("method", "Files.GetSources").put("id", 1)
    .put("params", new JSONObject().put("media", "music"));

Chaining .put() like this is possible because put() returns the object it was called on - for this exact purpose.

Tags:

Java

Json

Android