48

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?

santBart
  • 2,466
  • 9
  • 43
  • 66
  • JSONObject json = new JSONObject(); json.put("name", "emil"); json.put("username", "emil111"); json.put("age", "111"); – santBart Jan 02 '12 at 22:41

2 Answers2

93

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);
Smar
  • 8,109
  • 3
  • 36
  • 48
aromero
  • 25,681
  • 6
  • 57
  • 79
0

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"}}
Samiya Khan
  • 241
  • 3
  • 4