Reputation: 2496
I wish to create json looking like:
{"man":
{
"name":"emil",
"username":"emil111",
"age":"111"
}
}
within android. This is what I have so far:
JSONObject json = new JSONObject();
json.put("name", "emil");
json.put("username", "emil111");
json.put("age", "111");
Can anyone help me?
Upvotes: 48
Views: 44616
Reputation: 369
In kotlin, json object can be created lie this
val jsonObject = JSONObject()
val manObject = JSONObject()
manObject.put("name", "emil")
manObject.put("username", "emil111")
manObject.put("age", "111")
jsonObject.put("man", manObject)
Log.d("MIK", jsonObject.toString())
Output would look like this
{"man":{"name":"emil","username":"emil111","age":"111"}}
Upvotes: 0
Reputation: 25761
You can put another JSON object inside the main JSON object:
JSONObject json = new JSONObject();
JSONObject manJson = new JSONObject();
manJson.put("name", "emil");
manJson.put("username", "emil111");
manJson.put("age", "111");
json.put("man",manJson);
Upvotes: 93