Adrian Adrian - 8 months ago 47
Java Question

How to generate JSON Stringer in Android for this format

I need to send data to database in this format -

{"param1":"value1", "param2":"value2", "param3": {"username": "admin", "password": "123"}}

How to generate this using JSONStringer ?

I tried this -

vm = new JSONStringer().object().key("param1").value("value1")

But I'm getting this error -

org.json.JSONException: Nesting problem at

JSONObject object1 = new JSONObject();

object1.put("param1", "value1");
object1.put("param2", "param2");

JSONObject innerObject1 = new JSONObject();
innerObject1.put("username", "admin");
innerObject1.put("password", "123");


String jsonStr = object1.toString();

Ideally reverse of JSON parsing can be applied to create a json string object, so that the same can be send to Server/DB